面向对象编程思想
class Piece {
constructor(canvas) {
this.cs = canvas;
this.pRadius = 20;
}
log() {
console.log(this.pRadius);
}
}
class Checkerboard {
constructor(canvas,style) {
this.cs = canvas;
this.length = 50;
this.se=style;
}
draw() {
let width = parseInt(this.se.width);
let height = parseInt(this.se.height);
// let height = parseInt(this.cs.style.height);
this.cs.clearRect(0, 0, width, height);
this.cs.save();
this.cs.fillStyle='rgb(174, 107, 45)'
this.cs.fillRect(0, 0, width, height);
console.log(width, height);
this.cs.restore();
for (let i = 0; i < 12; i++) {
for (let j = 0; j < 12; j++) {
this.cs.strokeRect(i * this.length, j * this.length, (i + 1) * this.length, (j + 1) * this.length)
if (i == 3 && j == 3||i==9&&j==3||i==3&&j==9||i==9&&j==9||i==6&&j==6) {
this.cs.beginPath();
this.cs.arc(i * this.length, j * this.length, 3, 0, 2 * Math.PI, false);
this.cs.fill();
}
}
}
}
}
document.ready = init;
function init() {
let c1 = document.getElementById('c1');
let c1_style=window.getComputedStyle(c1,null);
if (c1.getContext) {
c1 = c1.getContext('2d');
}
let cb1 = new Checkerboard(c1,c1_style);
cb1.draw();
let p1 = new Piece(c1);
p1.log();
}
@沉木