In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how HTML5 draws complex shapes in canvas. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
First, draw complex shapes or paths
In cases where a simple rectangle does not meet the requirements, the drawing environment provides the following methods to draw complex shapes or paths.
BeginPath (): start drawing a new path.
ClosePath (): closes the shape by drawing a segment from the current point to the beginning of the path.
Fill (), stroke (): fill a shape or draw a hollow shape.
MoveTo (): moves the current point to the point (xcenture y).
LineTo (): draws a straight line from the current point to the point (xcenture y).
Arc: draw an arc to a point with a specified radius (xmeme y).
Second, using these methods to draw complex shapes should follow the following steps
Start drawing the path using the beginPath () method.
Create segments using the moveTo (), lineTo (), arc (), method.
Optionally, use closePath () to end the drawing and close the shape.
Use stroke () or fill () to draw the outer border or fill shape of the path. Using fill () automatically closes all unclosed paths.
Third, arc arc () drawing instructions
Drawing complex shapes in canvas
The code is as follows:
HTML5
/ / this function will be called after the page is fully loaded
Function pageLoaded ()
{
/ / get the reference to the canvas object. Note that the tCanvas name must be the same as the id in the body below.
Var canvas = document.getElementById ('tCanvas')
/ / get the 2D drawing environment of the canvas
Var context = canvas.getContext ('2d')
/ / the drawing code will appear here
/ / draw a complexity pig
/ / fill the triangle
Context.beginPath ()
Context.moveTo (10120); / / starting from (10pm 20)
Context.lineTo (10180); / / starts from (10120) and ends with (10180).
Context.lineTo (110150); / / starts from (10180) and ends with (110150).
Context.fill (); / / close the shape and draw it as a fill
/ / the outer border of the triangle
Context.beginPath ()
Context.moveTo (140160); / / start from point (140160)
Context.lineTo (140220)
Context.lineTo (40190)
Context.closePath (); / / close the path
Context.stroke (); / / filled with hollow
/ / A complex polygon
Context.beginPath ()
Context.moveTo (160160); / / start from point (160160)
Context.lineTo (170220)
Context.lineTo (240210)
Context.lineTo (260170)
Context.lineTo (190140)
Context.closePath ()
Context.stroke ()
/ / draw an arc
/ / draw a semicircle
Context.beginPath ()
/ draw an arc counterclockwise at (100300) with a radius of 40 and an angle from 0 to 180 °
Context.arc (100, 300, 40, 0) Math.Pi _ (1)); / / the Radian of PI is 180 °
Context.stroke ()
/ / draw a solid circle
Context.beginPath ()
/ draw an arc counterclockwise at (100300) with a radius of 30 and an angle of 0 to 360 °
Context.arc. / / 2*Math.PI is 360deg.
Context.fill ()
/ / draw a 3-stroke-4 arc
Context.beginPath ()
/ draw an arc clockwise at (200300) with a radius of 25 and an angle of 0 to 270 °
Context.arc (200, 300, 25, 50, 000, 3, 3, 2, 3, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 4, 2, 4, 2, 4, 3, 2, 4, 3, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 4, 2, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
Context.stroke ()
}
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.