Graphic combination of HTML5 canvas Basic Drawing, html5canvas
<Canvas> </canvas>It is just a container for drawing graphics. In addition to attributes such as id, class, and style, there are also attributes of height and width. There are three steps to draw an element on the <canvas> element:
1. Obtain the DOM object corresponding to the <canvas> element. This is a Canvas object;
2. Call the getContext () method of the Canvas object to obtain a CanvasRenderingContext2D object;
3. Call the CanvasRenderingContext2D object for plotting.
Graphic combination:
• GlobalAlpha:Sets or returns the current alpha or transparency value of the drawing.
This method is mainly used to set the image transparency, which is not described here.
• GlobalCompositeOperation:Set or return how the new image is drawn to an existing image. This method has the following attribute values:
The following is a small example. You can click to change the combination effect:
Copy XML/HTML Code to clipboard
- <! DOCTYPE html>
- <Html lang = "en">
- <Head>
- <Meta charset = "UTF-8">
- <Title> graphic combination </title>
- <Style type = "text/css">
- # Canvas {
- Border: 1px solid # 1C0EFA;
- Display: block;
- Margin: 20px auto;
- }
- # Buttons {
- Width: 1000px;
- Margin: 5px auto;
- Clear: both;
- }
- # Buttons {
- Font-size: 18px;
- Display: block;
- Float: left;
- Margin-left: 20px;
- }
- </Style>
- </Head>
- <Body>
- <Canvas id = "canvas" width = "1000" height = "800">
- Your browser does not support canvas.
- </Canvas>
- <Div id = "buttons">
- <A href = "#"> source-over </a>
- <A href = "#"> source-atop </a>
- <A href = "#"> source-in </a>
- <A href = "#"> source-out </a>
- <A href = "#"> destination-over </a>
- <A href = "#"> destination-atop </a>
- <A href = "#"> destination-in </a>
- <A href = "#"> destination-out </a>
- <A href = "#"> lighter </a>
- <A href = "#"> copy </a>
- <A href = "#"> xor </a>
- </Div>
- </Body>
- <Script type = "text/javascript">
- Window. onload = function (){
- Draw ("source-over ");
- Var buttons = document. getElementById ("buttons"). getElementsByTagName ("");
- For (var I = 0; I <buttons. length; I ++ ){
- Buttons [I]. onclick = function (){
- Draw (this. text );
- Return false;
- };
- }
- };
- Function draw (compositeStyle ){
- Var canvas = document. getElementById ("canvas ");
- Var context = canvas. getContext ("2d ");
- Context. clearRect (0, 0, canvas. width, canvas. height );
- // Draw title
- Context. font = "bold 40px Arial ";
- Context. textAlign = "center ";
- Context. textBasedline = "middle ";
- Context. fillStyle = "# 150E0E ";
- Context. fillText ("globalCompositeOperation =" + compositeStyle, canvas. width/2, 60 );
- // Draw a rect
- Context. fillStyle = "# F6082A ";
- Context. fillRect (300,150,500,500 );
- // Draw a triangle
- Context. globalCompositeOperation = compositeStyle;
- Context. fillStyle = "#1611F5 ";
- Context. beginPath ();
- Context. moveTo (700,250 );
- Context. lineTo (1000,750 );
- Context. lineTo (400,750 );
- Context. closePath ();
- Context. fill ();
- }
- </Script>
- </Html>
You can click the tag to observe the effect of different combinations. The effect is as follows:
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.