Home Web Front-end H5 Tutorial Canvas game development learning part one: First introduction to the tag

Canvas game development learning part one: First introduction to the tag

Jan 16, 2017 pm 04:52 PM
canvas Label game development

Let’s start with the definition of the element.

<canvas id="myCanvas" width="150" height="150"></canvas>
Copy after login

looks very similar to , the only difference is that it does not contain the src
and alt
attributes. It has only two properties, width and height, both of which are optional and can be set using DOM or CSS. If width and height are not specified, the default is 300 pixels wide and 150 pixels high. Although the canvas can be resized through CSS, the rendered image will scale to fit the layout (if you find that the rendering results look distorted, instead of relying solely on CSS, try explicitly specifying the width and height properties of the canvas).
The closing tag is required. The

element can be styled (margins, borders, background, etc.) just like a normal image. However, these styles will not have any impact on the actual image generated by canvas. Next we'll see how to apply styles. If no style is specified, the canvas is fully transparent by default.

Because
is relatively new and some browsers have not implemented it, such as Firefox 1.0 and Internet Explorer, so we need to provide alternative display content for browsers that do not support canvas. We just need to insert the replacement content directly into the canvas element. Browsers that do not support canvas will ignore the canvas element and render the alternative content directly, while browsers that support it will render the canvas normally. For example, we can fill some text or pictures into canvas as alternative content:

<canvas id="game" width="150" height="150">
  Oh dear, your browser dosen&#39;t support HTML5! Tell you what, why don&#39;t you upgrade to a decent browser - you won&#39;t regret it!</canvas>

<canvas id="clock" width="150" height="150">
  <imgsrc="images/clock.png" width="150" height="150"/>
</canvas>
Copy after login

The fixed-size drawing screen created opens one or more rendering contexts (rendering context), through which we can control the content to be displayed. We focus on 2D rendering, which is currently the only option, and may add an OpenGL ES-based 3D context in the future.

The initialization is blank. To draw with a script on it, you first need its rendering context. It can be obtained through the getContext
method of the canvas element object. At the same time, the obtained There are some functions for drawing. getContext()
Accepts a value describing its type as a parameter. getContext() returns a CanvasRenderingContext2D object.

var canvas = document.getElementById(&#39;myCanvas&#39;);
var ctx = canvas.getContext(&#39;2d&#39;);
Copy after login


The first line above obtains the DOM node of the canvas object through the getElementById method. Then obtain its drawing operation context through its getContext
method.

In addition to displaying alternative content on browsers that do not support canvas, you can also use scripts to check whether the browser supports canvas. The method is very simple, just determine whether getContext
exists.
var canvas = document.getElementById(&#39;myCanvas&#39;);
if (canvas.getContext){
  var ctx = canvas.getContext(&#39;2d&#39;);
  // drawing code here
} else {
  // canvas-unsupported code here
}
Copy after login

We will start with the following simplest code template (which will be used in subsequent examples).


  
    Canvas tutorial
    
    
  
  
    <canvas id="myCanvas" width="150" height="150"></canvas>
  
Copy after login

If you are careful, you will find that I have prepared a function named draw
, which will be executed once after the page is loaded (by setting the onload attribute of the body tag). Of course, it can also be used in other is called in the event handler function.

The above is one of canvas game development learning: first introduction to the content of the tag. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to search for text across all tabs in Chrome and Edge How to search for text across all tabs in Chrome and Edge Feb 19, 2024 am 11:30 AM

This tutorial shows you how to find specific text or phrases on all open tabs in Chrome or Edge on Windows. Is there a way to do a text search on all open tabs in Chrome? Yes, you can use a free external web extension in Chrome to perform text searches on all open tabs without having to switch tabs manually. Some extensions like TabSearch and Ctrl-FPlus can help you achieve this easily. How to search text across all tabs in Google Chrome? Ctrl-FPlus is a free extension that makes it easy for users to search for a specific word, phrase or text across all tabs of their browser window. This expansion

Build amazing games with Go Build amazing games with Go Apr 08, 2024 am 10:24 AM

Building amazing games using Go involves the following steps: Setting up the project: Create a new project using Git and create the necessary files. Write game logic: Write core game logic in game.go, such as guessing number games. Write the entry point: Create the entry point of the game in main.go, allowing user input and handling guesswork. Compile and run: Compile and run the game. The practical example is a guessing number game. The user can input numbers between 0 and 99 and get feedback.

Master how Golang enables game development possibilities Master how Golang enables game development possibilities Mar 16, 2024 pm 12:57 PM

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

How to choose a Java framework for game development How to choose a Java framework for game development Jun 06, 2024 pm 04:16 PM

When choosing a Java framework in game development, you should consider the specific needs of your project. Available Java game frameworks include: LibGDX: suitable for cross-platform 2D/3D games. JMonkeyEngine: used to build complex 3D games. Slick2D: Suitable for lightweight 2D games. AndEngine: A 2D game engine developed specifically for Android. Kryonet: Provides network connection capabilities. For 2DRPG games, for example, LibGDX is ideal because of its cross-platform support, lightweight design, and active community.

How to add tags on Douyin to attract traffic? Which tags on the platform are easiest to attract traffic to? How to add tags on Douyin to attract traffic? Which tags on the platform are easiest to attract traffic to? Mar 22, 2024 am 10:28 AM

As a popular short video social platform, Douyin has a huge user base. For Douyin creators, using tags to attract traffic is an effective way to increase the exposure of content and attract attention. So, how does Douyin use tags to attract traffic? This article will answer this question in detail for you and introduce related techniques. 1. How to add tags on Douyin to attract traffic? When posting a video, make sure to choose tags that are relevant to the content. These tags should cover the topic and keywords of your video to make it easier for users to find your video through tags. Leveraging popular hashtags is an effective way to increase your video’s exposure. Research current popular tags and trends and incorporate them into your video descriptions and tags. These popular tags usually have higher visibility and can attract the attention of more viewers. 3. Label

What is the clock behind the TikTok label? How to tag Douyin account? What is the clock behind the TikTok label? How to tag Douyin account? Mar 24, 2024 pm 03:46 PM

When browsing Douyin works, we often see a clock icon behind the tag. So, what exactly is this clock? This article will focus on the discussion of &quot;What is the clock behind the Douyin label&quot;, hoping to provide some useful reference for your use of Douyin. 1. What is the clock behind the Douyin label? Douyin will launch some hot topic challenges. When users participate, they will see a clock icon after the tag, which means that the work is participating in the topic challenge and displays the remaining time of the challenge. For some time-sensitive content, such as holidays, special events, etc., Douyin will attach a clock icon after the label to remind users of the validity period of the content. 3. Popular tags: When a tag becomes popular, Douyin will add a clock icon after the tag to indicate that the tag is

Unleash game creativity with Go language Unleash game creativity with Go language Apr 07, 2024 pm 04:39 PM

To create a 2D game using Go language, follow the following steps: Install Go language. Create a project directory and initialize the Go module. Create a game engine to handle graphics and input. Create a game object. Write the main game program. run game.

Practical cases of golang framework in game development Practical cases of golang framework in game development Jun 02, 2024 am 09:23 AM

Practical cases of Go framework in game development: Technology stack: Gov1.18, Gin framework, MongoDB architecture: Web server (processing HTTP requests), game server (processing game logic and communication), MongoDB database (storing player data) Web server : Use Gin routing to handle player creation and acquisition requests Game server: Handle game logic and player communication, use UNIX sockets for network communication Database: Use MongoDB to store player data, provide the function of creating and obtaining player information Actual case function: create players , obtain players, update player status, and handle player interactions. Conclusion: The Go framework provides efficient

See all articles