Komentar: Artikel ini terutama memperkenalkan dua contoh kode animasi rotasi kanvas HTML5, dan menyadari efek diagram Tai Chi yang berputar. Teman yang belajar animasi rotasi kanvas HTML5 dapat merujuknya.
Gambar reproduksi:
Metode 1:
<Html>
<body>
<Canvas> browser y tidak mendukung tag Canvas </ Canvas>
<type skrip = "Teks/JavaScript">
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 = "merah";
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);
</script> </p> <p> </body>
</html>
Metode 2:
<! Doctype html>
<Html>
<body>
<an Canvas> browser Anda tidak mendukung tag Canvas </ Canvas>
<type skrip = "Teks/JavaScript">
var canvas = document.geteLementById ('myCanvas');
var ctx = canvas.getContext ("2d");
var sudut = 0;
var count = 360;
var clra = '#000';
var clrb = 'merah';
fungsi taiji (x, y, jari -jari, sudut, bijak) {
Angleanggle = sudut || 0;
Wisewise = bijaksana? 1: -1;
ctx.save ();
ctx.translate (x, y);
ctx.rotate (sudut);
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 (bijak * -0.5 * radius, 0, radius / 2, 0, math.pi * 2, true);
ctx.fill ();
ctx.beginpath ();
ctx.fillstyle = clra;
ctx.arc (bijak * +0.5 * radius, 0, radius / 2, 0, math.pi * 2, false);
ctx.arc (bijak * -0.5 * radius, 0, radius / 10, 0, math.pi * 2, true);
ctx.fill ();
ctx.beginpath ();
ctx.fillstyle = clrb;
ctx.arc (bijak * +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);
sudut = (sudut + 5) % jumlah;
}, 50);
</script> </p> <p> </body>
</html>