HTML5-canvas instance: Scratch game, html5-canvas scratch

Source: Internet
Author: User

HTML5-canvas instance: Scratch game, html5-canvas scratch

Implementation Method:

(1) using the canvas, fillRect () depicts a rectangle (not transparent), positioned on a tag such as a div (the tag contains the winning information)

(2) globalCompositeOperation = 'destination-out'; using this attribute, draw your finger across the canvas, arc (x, y, radius, 0, Math. PI * 2, true) to draw a circle, the circle will penetrate the previously depicted rectangle and see the content of the div label.

GlobalCompositeOperation attributes: 

The globalCompositeOperation attribute sets or returns how to draw a source (new) image to an existing image.

Source image = the drawing you plan to place on the canvas.

Target Image = the drawing you have placed on the canvas.

Value Description
Source-over Default value. Displays the source image on the target image.
Source-atop The source image is displayed at the top of the target image. The source image is invisible outside the target image.
Source-in Displays the source image in the target image. Only the source image in the target image is displayed, and the target image is transparent.
Source-out Displays the source image outside the target image. Only the part of the source image outside the target image is displayed. The target image is transparent.
Destination-over The target image is displayed above the source image.
Destination-atop The target image is displayed at the top of the source image. The part of the target image outside the source image is not displayed.
Destination-in Displays the target image in the source image. Only the target image in the source image is displayed, and the source image is transparent.
Destination-out Displays the target image outside the source image. Only the target image outside the source image is displayed, and the source image is transparent.
Lighter Displays the source image + target image.
Copy Displays the source image. Ignore the target image.
Source-over Use an exclusive or operation to combine the source image and the target image.

For example, blue indicates the target image (that is, the rectangle), and red indicates the source image (that is, the circle that the finger is crossed)

 

We can see that globalCompositeOperation = 'destination-out' is the property we want.

Complete sample code:

<! DOCTYPE html> 

 

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.