Html5 implements scratch on mobile terminals and PCs, and html5 scratch on cards
First, we will show you:
Scratch card requirements:
Each user has three chances of scratch cards.
The result of this scratch will overwrite the previous result.
The chance of winning a scratch card is shown in an ascending curve (ensure that one win is required in three times)
The scraped result contains both buttons (receive the prize or try again)
Prize upgrade for sharing activities (the callback for sharing is used here)
We have our own requirements. Today we will talk about how to create scratch cards. If you have such requirements, please contact me for the source code.
1. Create a Canvas with the body
Copy XML/HTML Code to clipboard
- <Div class = "info" id = "prize">
- <Span id = "prompt"> </span>
- <Span class = "btn" id = "OK"> receive prize </span>
- <Span class = "btn" id = "no"> try again </span>
- </Div>
- <Canvas id = "c1" class = "canvas"> </canvas>
Here, we first create a Canvas and add the scratch effect to the bottom of the canvas.
2. initialize the canvas after page loading
First, define some required variables.
Copy XML/HTML Code to clipboard
- Var c1; // canvas
- Var ctx; // paint brush
- Var ismousedown; // indicates whether the user clicks the mouse or starts to touch
- Var isOk = 0; // indicates whether more than half of the user has been scratched
- Var fontem = parseInt(window.getComputedStyle(document.doc umentElement, null) ["font-size"]); // This is used with @ media to automatically adjust the width of the scratch at different resolutions
Initialize the canvas after the page is loaded (you can create a canvas on the page)
Copy XML/HTML Code to clipboard
- Window. onload = function (){
- C1 = document. getElementById ("c1 ");
- // The key here is that canvas has two attributes: width and height. I think the canvas resolution is different from width and height in style.
- // It is best to set the size to the same as the actual size of the canvas on the page.
- // Otherwise, the coordinates in the canvas cannot match the coordinates of the mouse.
- C1c1. width = c1.clientWidth;
- C1c1. height = c1.clientHeight;
- Ctx = c1.getContext ("2d ");
- // PC-side Processing
- C1.addEventListener ("mousemove", eventMove, false );
- C1.addEventListener ("mousedown", eventDown, false );
- C1.addEventListener ("mouseup", eventUp, false );
- // Mobile terminal Processing
- C1.addEventListener ('touchstart', eventDown, false );
- C1.addEventListener ('touchend', eventUp, false );
- C1.addEventListener ('touchmove ', eventMove, false );
- // Initialization
- InitCanvas ();
- }
3. Fill the gray rectangle
Copy XML/HTML Code to clipboard
- Function initCanvas () {// The online method is to set a background image for the canvas. Here, I put a div directly under the canvas.
- // C1.style. backgroundImage = "url(.jpg )";
- Ctx. globalCompositeOperation = "source-over ";
- Ctx. fillStyle = '# aaaaaa ';
- Ctx. fillRect (0, 0, c1.clientWidth, c1.clientHeight );
- Ctx. fill ();
- Ctx. font = "Bold 30px Arial ";
- Ctx. textAlign = "center ";
- Ctx. fillStyle = "#999999 ";
- Ctx. fillText ("Scratch", c1.width/); // set this attribute to make a circular eraser.
- // Some old mobile Browsers Do not support destination-out. The following code provides a solution.
- Ctx. globalCompositeOperation = 'destination-out ';}
4. Press the mouse and start with touch
Copy XML/HTML Code to clipboard
- Function eventDown (e ){
- E. preventDefault ();
- Ismousedown = true ;}
5. Move the mouse up and touch the end
Copy XML/HTML Code to clipboard
- Function eventUp (e ){
- E. preventDefault ();
- // Obtain all the data of the canvas.
- Var a = ctx. getImageData (0, 0, c1.width, c1.height );
- Var j = 0;
- For (var I = 3; I <a. data. length; I + = 4 ){
- If (a. data [I] = 0) j ++;
- }
- // When the split area is equal to half, the processing result can be started.
- If (j> = a. data. length/8 ){
- IsOk = 1;
- }
- Ismousedown = false;
- }
6. Move the mouse and touch
Copy XML/HTML Code to clipboard
- // Move the mouse and touch
- Function eventMove (e ){
- E. preventDefault ();
- If (ismousedown ){
- If (e. changedTouches ){
- Ee = e. changedTouches [e. changedTouches. length-1];
- }
- Var topY = document. getElementById ("top"). offsetTop;
- Var oX = c1.offsetLeft,
- OY = c1.offsetTop + topY;
- Var x = (e. clientX + document. body. scrollLeft | e. pageX)-oX | 0,
- Y = (e. clientY + document. body. scrollTop | e. pageY)-oY | 0;
- // Draw an arc of 360 degrees, which is a circle because ctx. globalCompositeOperation = 'destination-out' is set ';
- // The image is transparent.
- Ctx. beginPath ();
- Ctx. arc (x, y, fontem * 1.2, 0, Math. PI * 2, true );
- // The following three lines of code are used to fix the fact that some mobile Browsers Do not support destination-out.
- // I am not very clear about the principle of doing so.
- C1.style. display = 'none ';
- C1.offsetHeight;
- C1.style. display = 'herit ';
- Ctx. fill ();
- }
- If (isOk ){
- Var btn = document. getElementsByClassName ("btn ");
- For (var I = 0; I <btn. length; I ++ ){
- Btn [I]. style. zIndex = '3 ';
- }
- Document. getElementsByClassName ("btn") [0]. style. zIndex = "3 ";
- }
- }
7. Try again if you have not selected
Obviously, the next time is to initialize all values and reload the canvas. However, if you need to limit the number of times, make sure to calculate it clearly here.
Note: As we have many requirements, today we will not discuss how to calculate the winning probability.
The above is a small part of the Html5 Implementation of the mobile terminal, PC side scratch card effect, I hope to help you, if you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!