Example of the scratch Effect of html5 using canvas

Source: Internet
Author: User

Comments: This article mainly introduces the example of using canvas to implement the scratch Effect of html5. For more information, see.

The coating automatically adapts to the size of the new image when img. src is modified.

Modify the layer function to change the coating style.

The following is the HTML source code (supported by mobile devices ):


The Code is as follows:
<! DOCTYPE html>
<Html>
<Body>
<Canvas/>
<Script>
(Function (bodyStyle ){
BodyStyle. Required userselect = 'none ';
BodyStyle. webkitUserSelect = 'none ';

Var img = new Image ();
Var canvas = document. querySelector ('canvas ');
Canvas. style. backgroundColor = 'transparent ';
Canvas. style. position = 'absolute ';

Img. addEventListener ('load', function (e ){
Var ctx;
Var w = img. width,
H = img. height;
Var offsetX = canvas. offsetLeft,
OffsetY = canvas. offsetTop;
Var mousedown = false;

Function layer (ctx ){
Ctx. fillStyle = 'Gray ';
Ctx. fillRect (0, 0, w, h );
}

Function eventDown (e ){
E. preventDefault ();
Mousedown = true;
}

Function eventUp (e ){
E. preventDefault ();
Mousedown = false;
}

Function eventMove (e ){
E. preventDefault ();
If (mousedown ){
If (e. changedTouches ){
E = e. changedTouches [e. changedTouches. length-1];
}
Var x = (e. clientX + document. body. scrollLeft | e. pageX)-offsetX | 0,
Y = (e. clientY + document. body. scrollTop | e. pageY)-offsetY | 0;
With (ctx ){
BeginPath ()
Arc (x, y, 5, 0, Math. PI * 2 );
Fill ();
}
}
}

Canvas. width = w;
Canvas. height = h;
Canvas. style. backgroundImage = 'url ('+ img. src + ')';
Ctx = canvas. getContext ('2d ');
Ctx. fillStyle = 'transparent ';
Ctx. fillRect (0, 0, w, h );
Layer (ctx );

Ctx. globalCompositeOperation = 'destination-out ';

Canvas. addEventListener ('touchstart', eventDown );
Canvas. addEventListener ('touchend', eventUp );
Canvas. addEventListener ('touchmove ', eventMove );
Canvas. addEventListener ('mousedown ', eventDown );
Canvas. addEventListener ('mouseup', eventUp );
Canvas. addEventListener ('mousemove ', eventMove );
});
Img. src = 'data: image/png; base64, expires + expires/expires + gDefUO4flpP56 // vv + 9693/3 u/hour + average/Xw + average/xtHWht7/p2Ip/Baidu/Bo/Vn9QgY + weight/zgan6Rx2X + cy9V6W + authorization + signature/Signature + 3XWTxIScO14J/5jpA2DHYkhXvVisWM/authorization + L + signature + + yAlyRxsI0/authorization + 7/authorization + signature/Signature + GHDjdC8eIY/authorization + Jgop + Signature /release/T + release + 2 release/release + 24bbg1wPfE + release + + qDvWixgT8ZI4H/zTQGgBG + release + z + release/VY4U + release/release /examples/templates + examples/samples/fKk/templates/CvI5V + examples + y4PFGZYg/samples + examples/templates + bMqqQY5S + examples/samples + DT6qUJBiNuHxBV5cN8CCM/templates + MI8Ez + kernel + a5YX/kernel/SHSsPLf/NKW/kernel + kernel/z6lw + kernel + 3nht5 + P + kernel/vcqL/kernel + z21I3t0inVGaIe6AXpqTo/ release/release + release/LZ3tONvYm5x/l + E + Ekhlc9cyc5 + release/PJN/release ++ release + release/Y + utaOVDVh/a4yvKsQYrn/3cz + 383667j/release + release/BpiARkxL/M9ODO4aGNN + rGiHfbeaYWK/release + release/r/release + release/HL + V7cQp74Ya/EqSt0Pzy2y + release /clusters/packages/5K7ZWF3bwOx + packages + I2HpqfssIVqz + packages/packages + downloads/downloads + FMPzbDjAco + logs/XZAKJ89h1Ht + downloads /Qv + examples/examples + examples/JOw + examples/Uu40t/examples/B7ylwC/EDTM/+ examples + vSWvPCN1GPHbXvaK9Ry/examples/JdD6LFS/ listen + Z58Q + DYnti7UtxkOoyf/weight + weight/weight + dw6kp/pWXX5 + V4vKAVWv + PqCgZTjMWU/y/kV1qIqOF/weight/ub2aboj/Weight = ';
}) (Document. body. style );
</Script>
</Body>
</Html>

You need to determine whether to use this code to replace the eventUp event handler function of the original code when it is finished:


The Code is as follows:
E. preventDefault ();
Mousedown = false;
Var data = ctx. getImageData (0, 0, w, h). data;
For (var I = 0, j = 0; I <data. length; I ++ = 4 ){
If (data [I] & data [I + 1] & data [I + 2] & data [I + 3]) {
J ++;
}
}
If (j <= w * h * 0.1 ){
Alert ('OK ');
}

In this Code, 0.1 means 10%. When the area of the coating is less than or equal to 10%, a window is displayed, indicating that the coating is finished and can be adjusted as needed.


Related Article

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.