How to realize HTML5 Canvas rotation Animation
This article introduces the relevant knowledge of "how to achieve HTML5 Canvas rotation animation". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Effect picture:
Method 1:
The code is as follows:
Your browser does not support the canvas tag
Var deg = 0
Var r = 30
Var rl = 100
Function drawTaiji () {
Var canvas = document.getElementById ('myCanvas')
Var context = canvas.getContext ('2d')
Var colorA = "rgb (0,0,0)"
Var colorB = "red"
Var px = Math.sin (deg) * r
Var py = Math.cos (deg) * r
Context.clearRect (0,0,300,300)
Context.beginPath ()
Context.fillStyle = colorA
Context.arc (rl, rl, 60,0.5 * Math.PI + deg, 1.5 * Math.PI + deg, true)
Context.closePath ()
Context.fill ()
Context.fillStyle = colorB
Context.beginPath ()
Context.arc (rl, rl, 60,1.5 * Math.PI + deg, 0.5 * Math.PI + deg, true)
Context.closePath ()
Context.fill ()
Context.fillStyle = colorB
Context.beginPath ()
Context.arc (rl+px, rl-py, 30,0.5 * Math.PI + deg, 1.5 * Math.PI + deg, true)
Context.closePath ()
Context.fill ()
Context.fillStyle = colorA
Context.beginPath ()
Context.arc (rl-px, rl+py, 30,1.5 * Math.PI + deg, 0.5 * Math.PI + deg, true)
Context.closePath ()
Context.fill ()
Context.fillStyle = colorA
Context.beginPath ()
Context.arc (rl+px, rl-py, 8, 0, 2 * Math.PI, true)
Context.closePath ()
Context.fill ()
Context.fillStyle = colorB
Context.beginPath ()
Context.arc (rl-px, rl+py, 8, 0, 2 * Math.PI, true)
Context.closePath ()
Context.fill ()
Deg + = 0.1
}
SetInterval (drawTaiji, 100)
Method 2:
The code is as follows:
Your browser does not support the canvas tag
Var canvas = document.getElementById ('myCanvas')
Var ctx = canvas.getContext ("2d")
Var angle = 0
Var count = 360
Var clrA ='# 000'
Var clrB = 'red'
Function taiji (x, y, radius, angle, wise) {
Angleangle = angle | | 0
Wisewise = wise? 1:-1
Ctx.save ()
Ctx.translate (x, y)
Ctx.rotate (angle)
Ctx.fillStyle = clrA
Ctx.beginPath ()
Ctx.arc (0,0, radius, 0, Math.PI, true)
Ctx.fill ()
Ctx.beginPath ()
Ctx.fillStyle = clrB
Ctx.arc (0,0, radius, 0, Math.PI, false)
Ctx.fill ()
Ctx.fillStyle = clrB
Ctx.beginPath ()
Ctx.arc (wise *-0.5 * radius, 0, radius / 2,0, Math.PI * 2, true)
Ctx.fill ()
Ctx.beginPath ()
Ctx.fillStyle = clrA
Ctx.arc (wise * + 0.5 * radius, 0, radius / 2,0, Math.PI * 2, false)
Ctx.arc (wise *-0.5 * radius, 0, radius / 10,0, Math.PI * 2, true)
Ctx.fill ()
Ctx.beginPath ()
Ctx.fillStyle = clrB
Ctx.arc (wise * + 0.5 * radius, 0, radius / 10,0, Math.PI * 2, true)
Ctx.fill ()
Ctx.restore ()
}
Loop = setInterval (function () {
BeginTag = true
Ctx.clearRect (0,0, canvas.width, canvas.height)
Taiji (200,200,50, Math.PI * (angle / count) * 2, true)
/ taiji (350,350,50, Math.PI * ((count-angle) / count) * 2, false)
Angle = (angle + 5)% count
}, 50)
"how to achieve HTML5 Canvas rotation animation" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!