Komentar: Artikel ini terutama memperkenalkan contoh kode grafik Jagged Canvas HTML5. Teman yang membutuhkannya bisa merujuknya.
Gambar reproduksi:
Kiat: Salin kode ke file HTML dan simpan, dan buka langsung untuk melihat efeknya.
Kode Implementasi:
<html lang = "zh">
<head>
<meta charset = "gbk">
<title> grafik Sawtoothed </iteme>
<type skrip = "Teks/JavaScript">
window.addeventListener ("muat", eventWindowloaded, false);
function eventWindowloaded () {
var x, y;
var thecanvas = document.getElementById ("canvas");
var context = thecanvas.getContext ("2d");
//Kotak
context.strokestyle = '#00f';
Context.linewidth = 10;
Context.strokerect (0, 0, thecanvas.width-0, thecanvas.height-0);
context.fillstyle = "#00f";
untuk (x = 5; x <= canvas.width; x = x+10) {
context.beginpath ();
context.arc (x, 5,5,0, math.pi, false);
context.closepath ();
context.fill ();
context.beginpath ();
context.arc (x, canvas.height-5,5,0, math.pi, true);
context.closepath ();
context.fill ();
}
untuk (y = 5; y <= canvas.height; y = y+10) {
context.beginpath ();
context.arc (5, y, 5,0, math.pi*2, true);
context.arc (canvas.width-5, y, 5,0, math.pi*2, true);
context.closepath ();
context.fill ();
}
}
</script>
</head>
<body>
<div>
<an kanvas>
</div>
</body>
</html>