console
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Canvas API封装示例</title>
<style>
canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<canvas id="myCanvas" width="1920" height="200"></canvas>
<script>
function isMouseOverHandle(mouseX, mouseY) {
const dx = mouseX - handle.x;
const dy = mouseY - handle.y;
return Math.sqrt(dx * dx + dy * dy) <= handle.size;
}
const handle = {
x: 100,
y: 100,
size: 20
};
class CanvasAPI {
dragging=false;
constructor(canvasId,drawHandle) {
const canvas = document.getElementById(canvasId);
this.canvas=canvas;
canvas.addEventListener('mousedown', (event) => {
console.log(event,'event')
const e=event.target;
const mouseX = e.offsetX;
const mouseY = e.offsetY;
if (isMouseOverHandle(mouseX, mouseY)) {
this.dragging = true;
offsetX = mouseX - handle.x;
canvas.style.cursor = 'e-resize';
}
});
canvas.addEventListener('mousemove', (event) => {
console.log(event,'event')
const e=event.target;
const mouseX = e.offsetX;
const mouseY = e.offsetY;
if (this.dragging) {
handle.x = mouseX - offsetX;
console.log("重新弄")
drawHandle();
} else if (isMouseOverHandle(mouseX, mouseY)) {
canvas.style.cursor = 'e-resize';
} else {
canvas.style.cursor = 'default';
}
});
canvas.addEventListener('mouseup', () => {
this.dragging = false;
canvas.style.cursor = 'default';
});
if (!this.canvas) {
throw new Error(`Canvas with ID ${canvasId} not found.`);
}
this.ctx = this.canvas.getContext('2d');
}
setStyle(options = {}) {
const { lineWidth, strokeStyle, fillStyle, font, textAlign } = options;
if (lineWidth) this.ctx.lineWidth = lineWidth;
if (strokeStyle) this.ctx.strokeStyle = strokeStyle;
if (fillStyle) this.ctx.fillStyle = fillStyle;
if (font) this.ctx.font = font;
if (textAlign) this.ctx.textAlign = textAlign;
}
drawLine(x1, y1, x2, y2) {
this.ctx.beginPath();
this.ctx.moveTo(x1, y1);
this.ctx.lineTo(x2, y2);
this.ctx.stroke();
}
drawRect(x, y, width, height) {
this.ctx.beginPath();
this.ctx.rect(x, y, width, height);
this.ctx.fill();
this.ctx.stroke();
}
drawCircle(x, y, radius) {
this.ctx.beginPath();
this.ctx.arc(x, y, radius, 0, 2 * Math.PI);
this.ctx.fill();
this.ctx.stroke();
}
drawText(text, x, y) {
this.ctx.fillText(text, x, y);
}
clearCanvas() {
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
drawImage(image, x, y, width, height) {
this.ctx.drawImage(image, x, y, width, height);
}
drawPath(path) {
this.ctx.beginPath();
path.forEach((point, index) => {
if (index === 0) {
this.ctx.moveTo(point.x, point.y);
} else {
this.ctx.lineTo(point.x, point.y);
}
});
this.ctx.closePath();
this.ctx.fill();
this.ctx.stroke();
}
drawGradient(x1, y1, x2, y2) {
const gradient = this.ctx.createLinearGradient(x1, y1, x2, y2);
gradient.addColorStop(0, 'blue');
gradient.addColorStop(1, 'red');
this.ctx.fillStyle = gradient;
this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
}
setFillColor(color) {
this.ctx.fillStyle = color;
}
setStrokeColor(color) {
this.ctx.strokeStyle = color;
}
setFont(font) {
this.ctx.font = font;
}
}
function draw(){
canvas.setStyle({
lineWidth: 3,
strokeStyle: 'black',
fillStyle: 'lightblue',
font: '16px Arial',
textAlign: 'center'
});
canvas.setStyle({
lineWidth: 1,
});
const kdcH=20;
canvas.drawLine(0, 150, 1920, 150);
for(let i=1;i<20;i++){
canvas.drawLine(i*100, 150-kdcH, i*100, 150);
}
canvas.setStyle({
font: '12px Arial',
lineWidth: 1,
fillStyle: '#bbb'
});
for(let i=1;i<20;i++){
canvas.drawLine(i*100+50, 150 - kdcH/2, i*100+50, 150);
}
canvas.setStyle({
font: '12px Arial',
lineWidth: 1,
fillStyle: '#00f'
});
for(let i=1;i<20;i++){
canvas.drawText(i+':00', i*100, 170);
}
canvas.setStyle({
font: '12px Arial',
lineWidth: 1,
strokeStyle: '#f00',
fillStyle: '#f00'
});
canvas.drawLine(handle.x, 100, handle.x, 150);
}
const canvas = new CanvasAPI('myCanvas',draw);
draw();
</script>
</body>
</html>