HTML5 instances-simple implementation of functions similar to scratch cards

Source: Internet
Author: User


Note the key settings:


1. Set User scaling: User-scalable=no|yes

The code is as follows Copy Code
<meta name= "viewport" content= "Width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, User-scalable=no "/>


2. Prohibit dragging:

The code is as follows Copy Code
Document.ontouchmove = function (e) {E.preventdefault ();}; Document prohibits Touchmove events


3. Prohibit pop-up selection menu:
The code is as follows Copy Code

Document.documentElement.style.webkitTouchCallout = "None";




Now put the complete code, want to learn the students themselves to test it.
The code is as follows Copy Code


<! DOCTYPE html>

<meta charset= "UTF-8"/>
<meta content= "Width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;" name= "viewport"/>
<title> Zhi Ling Legends </title>
<style type= "Text/css" >
Body {
width:320px;
min-height:568px;
Overflow:hidden;
margin:0;
}
#canvas {
Background:url (img/lzl.jpg);
/* Prize Pictures/*
Fixed Center center no-repeat;
Background-size:cover;
width:320px;
min-height:480px;
Overflow:hidden;
position:relative;
}
. before {
Background:none!important;
}
#canvas Canvas {
Cursor:url ("Img/bird.png") 0 0, auto;
/*PC-End gesture Picture * *
}
</style>

<body oncontextmenu= ' return false; ' onselectstart= ' return false; >
<div id= "Canvas" >
</div>
</body>
<script type= "Text/javascript" >
(function () {

Window.onload = function () {
/** Prohibit drag settings */
Document.ontouchmove = function (e) {
E.preventdefault ();
};

/** to determine if the browser supports canvas**/

try {

Document.createelement (' canvas '). GetContext (' 2d ');

catch (e) {

var adddiv = document.createelement (' div ');

Alert (' Your phone does not support scraping card effect oh ~! ');

}

};

var u = navigator.useragent,
Mobile = ' PC ';

if (u.indexof (' iphone ') >-1) Mobile = ' iphone ';

if (U.indexof (' Android ') >-1 | | u.indexof (' Linux ') >-1) Mobile = ' Android ';

function Createcanvas (parent, width, height) {

var canvas = {};

Canvas.node = document.createelement (' canvas ');

Canvas.context = Canvas.node.getContext (' 2d ');
Here you can add yourself to the label
Canvas.node.width = Width | | 320;

Canvas.node.height = Height | | 480;
Add ID to Canvas label
canvas.node.id = ' Canvastag ';

Parent.appendchild (Canvas.node);

return canvas;

}

function init (container, width, height, fillcolor, type) {

var canvas = Createcanvas (container, width, height);

var ctx = Canvas.context;

Define a custom Fillcircle method

Ctx.fillcircle = function (x, y, radius, fillcolor) {

This.fillstyle = FillColor;

This.beginpath ();

This.moveto (x, y);

This.arc (x, y, radius, 0, Math.PI * 2, false);

This.fill ();

};

Ctx.clearto = function (FillColor) {

Ctx.fillstyle = FillColor;

Ctx.fillrect (0, 0, width, height);

};

Ctx.clearto (FillColor | | "#ddd");

Canvas.node.addEventListener (mobile = "PC"?) "MouseDown": "Touchstart", function (e) {

Canvas.isdrawing = true;

}, False);

Canvas.node.addEventListener (mobile = "PC"?) "MouseUp": "Touchend", function (e) {

Canvas.isdrawing = false;

}, False);

Canvas.node.addEventListener (mobile = "PC"?) "MouseMove": "Touchmove", function (e) {

if (!canvas.isdrawing) {

Return

}

if (type = = ' Android ') {

var x = E.changedtouches[0].pagex-this.offsetleft;

var y = e.changedtouches[0].pagey-this.offsettop;

} else {

var x = E.pagex-this.offsetleft;

var y = e.pagey-this.offsettop;

}

var radius = 20;

var fillcolor = ' #ff0000 ';

ctx.globalcompositeoperation = ' destination-out ';

Ctx.fillcircle (x, y, radius, fillcolor);

}, False);

}

var container = document.getElementById (' canvas ');

Init (container, 568, ' #696868 ', mobile);

})();
</script>

The browsing effect is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.