let x = 0,y = 0,width = 300,height = 200, offsetY = 0;
// 画个长方形,限制范围
ctx.save();
ctx.moveTo(x, y);
ctx.lineTo(x + width, y);
ctx.lineTo(x + width, y + height);
ctx.lineTo(x, y + height);
ctx.closePath();
ctx.fillStyle = "#000"
ctx.stroke();
// 以该长方形作为范围剪裁
ctx.clip();
for (let index = 0; index < 20; index++) { // 生成20行文字
ctx.font = `15px 黑体`;
ctx.fillText("xxx", x, y + index * 20 - offsetY);
}
ctx.restore();
最后通过 滚轮去控制offsetY就可以实现滚动啦,还可以做优化,不在框框内的文本不做渲染
网友评论