HTML5 has many features that can integrate multimedia into webpages. You can use the canvas Element to fill the blank canvas with lines, load image files, and even animation effects. In this article, I will create a slide puzzle game to demonstrate the image processing capabilities of HTML5canvas. In the network ...,. HTML5 has many features that can integrate multimedia into webpages. You can use the canvas Element to fill the blank canvas with lines, load image files, and even animation effects.
In this article, I will create a slide puzzle game to demonstrate the image processing capabilities of HTML5 canvas.
Use the canvas label in the webpage to create the canvas.
-
The width and height of the canvas are measured in pixels. If the two are not specified, their default width is 300px and the height is 150px. You need to use the canvas context to draw a graph. You can use a script to call getContext () to obtain the context. W3C defines it as two-dimensional, or 2d. To initialize the context, use the following method:
- Document. getElementById ("vanvas"). getContext ("2d ");
The next step is to display images on the canvas. The API only provides the drawImage () method. However, there are three call methods. The most common parameters are the input three parameters: the image object and the x and y coordinates of the image relative to the canvas.
- DrawImage (image, x, y );
You can also add two parameters to set the image width and height.
- DrawImage (image, x, y, width, height );
The most complex drawImage function has nine parameters: image object, image x coordinate, image y coordinate, image width, Image Height, target x coordinate, and target y coordinate, target width and height. The last four parameters are mainly used to capture the original image for display, such as local amplification and cutting. The above is the image processing method. Let's do an exercise.
-
- Easy
-
- Hard
-
The DIV above includes another HTML5 Tag: range input, which allows you to drag and drop the slider to select a value. Let's talk about how to interact with range input in the puzzle. So far, ie and firefox do not support this label.
Now, as I said above, we need context to draw images on the canvas.
- Var context = document. getElementById ("puzzle"). getContext ("2d ");
By the way, we also need an image, which is included in the example, or we can find an image of the same size as the canvas.
- Var img = new Image ();
- Img. src = 'HTTP: // www.brucealderman.info/images/dimetrodon.jpg ';
- Img. addEventListener ('load', drawTiles, false );
This event ensures that the image is loaded and then placed into the canvas. Next we use range input to set the number of puzzles. The data range is from 3 to 5 (several rows and columns ).
- Var boardSize = document. getElementById ('puzzle'). width;
- Var tileCount = document. getElementById ('Scale'). value;
With the above two values, you can calculate the size of a puzzle.
- Var tileSize = boardSize/tileCount;
OK. Let's start creating the canvas.
- Var boardParts = new Object;
- SetBoard ();
SetBoard () is used to initialize the dashboard. to simulate the display of this canvas, we use a two-dimensional array. However, the process of using JavaScript to create such an array is not very elegant. We first define a plane array, and each array then defines an array. In this puzzle game, each element is an object with the grid location where x and y coordinate records are located. Therefore, each object has two coordinates. The first coordinate is the array coordinate, indicating its position on the canvas. The other coordinate is the x and y attributes of the object, which records the position of the puzzle image. If the two coordinates are the same, the positions are correct.
To achieve this goal, we swap their locations during initialization. In this way, the puzzle is not in the correct position.
- Function setBoard (){
- BoardParts = new Array (tileCount );
- For (var I = 0; I <tileCount; ++ I ){
- BoardParts [I] = new Array (tileCount );
- For (var j = 0; j <tileCount; ++ j ){
- BoardParts [I] [j] = new Object;
- BoardParts [I] [j]. x = (tileCount-1)-I;
- BoardParts [I] [j]. y = (tileCount-1)-j;
- }
- }
- EmptyLoc. x = boardParts [tileCount-1] [tileCount-1]. x;
- EmptyLoc. y = boardParts [tileCount-1] [tileCount-1]. y;
- Solved = false;
- }
We have not defined the last three variables.
We must track the location of the blank puzzle and record the user's clicked location.
- Var clickLoc = new Object;
- ClickLoc. x = 0;
- ClickLoc. y = 0;
- Var
- EmptyLoc = new Object;
- EmptyLoc. x = 0;
- EmptyLoc. y = 0;
The final variable indicates whether the puzzle is complete.
- Var solved = false;
After finding the correct position for all the puzzles, set it to true.
Now we need some methods related to solving puzzles.
First, define a trigger event for rang input. When it changes, we need to recalculate the number and size of the puzzle.
- Document. getElementById ('Scale'). onchange = function (){
-
- TileCount = this. value;
- TileSize = boardSize/
- TileCount;
- SetBoard ();
-
- DrawTiles ();
- };
You also need to track the mouse passing through the puzzle and which one is clicked.
- Document. getElementById ('puzzle'). onmousemove = function (e)
- {
- ClickLoc. x = Math. floor (e. pageX-this. offsetLeft )/
- TileSize );
- ClickLoc. y = Math. floor (e. pageY-
- This. offsetTop)/tileSize );
- };
- Document. getElementById ('puzzle'). onclick
- = Function (){
- If (distance (clickLoc. x, clickLoc. y,
- EmptyLoc. x, emptyLoc. y) = 1 ){
-
- SlideTile (emptyLoc, clickLoc );
-
- DrawTiles ();
- }
- If (solved)
- {
- Alert ("You solved
- It! ");
- }
- };
Some browsers will pop up a dialog box before redrawing the canvas. to prevent it from happening, you must use a delay.
- If (solved ){
- SetTimeout (function () {alert ("You solved
- It! ") ;}, 500 );
- }
When a puzzle is clicked, we need to know whether its surroundings can be moved. The determination method is that the total distance from the current location to the blank location can be moved when it is 1.
To put it simply, if x is the same, you must determine whether the distance of y is 1, and if y is the same, you must determine whether the distance of x is 1.
- Function distance (x1, y1, x2, y2 ){
- Return Math. abs (x1-
- X2) + Math. abs (y1-y2 );
- }
The practice of moving a puzzle is to copy the coordinates of the clicked puzzle to an empty position. Then, set the click position to a blank coordinate.
- Function slideTile (toLoc, fromLoc ){
- If (! Solved)
- {
- BoardParts [toLoc. x] [toLoc. y]. x =
- BoardParts [fromLoc. x] [fromLoc. y]. x;
-
- BoardParts [toLoc. x] [toLoc. y]. y =
- BoardParts [fromLoc. x] [fromLoc. y]. y;
-
- BoardParts [fromLoc. x] [fromLoc. y]. x = tileCount-
- 1;
-
- BoardParts [fromLoc. x] [fromLoc. y]. y = tileCount-
- 1;
- ToLoc. x =
- FromLoc. x;
- ToLoc. y =
- FromLoc. y;
-
- CheckSolved ();
- }
- }
Once the puzzle moves, we need to check whether it is all in the correct position.
- Function checkSolved (){
- Var flag =
- True;
- For (var I = 0; I <tileCount; ++ I)
- {
- For (var j = 0; j <
- TileCount; ++ j)
- {
- If
- (BoardParts [I] [j]. x! = I | boardParts [I] [j]. y! = J)
- {
-
- Flag =
- False;
-
- }
- }
-
- }
- Solved = flag;
- }
If a function is incorrect, false is returned; otherwise, true is returned.
Finally, redraw the clicked puzzle to a new position.
- Function drawTiles (){
- Context. clearRect (0, 0, boardSize, boardSize );
- For (var I = 0; I <tileCount; ++ I ){
- For (var j = 0; j <tileCount; ++ j ){
- Var x = boardParts [I] [j]. x;
- Var y = boardParts [I] [j]. y;
- If (I! = EmptyLoc. x | j! = EmptyLoc. y | solved = true ){
- Context. drawImage (img, x * tileSize, y * tileSize,
- I * tileSize, j * tileSize, tileSize, tileSize );
- }
- }
- }
- }
When drawing a puzzle, this function prevents blank positions from being matched when filling the canvas, because you can choose different difficulty options in the game.