如何使用 JavaScript 在 x/y 位置绘制点

新手上路,请多包涵

我想以二维方式绘制点(每个点都有一个 x 和 y 坐标)。我想知道您是否知道这样做的库或项目,这样我就不必从头开始构建它。

原文由 Tam 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 341
2 个回答

使用 canvas 标签是最好的方法。下面是创建画布的示例:

 // Create a canvas that extends the entire screen
// and it will draw right over the other html elements, like buttons, etc
var canvas = document.createElement("canvas");
canvas.setAttribute("width", window.innerWidth);
canvas.setAttribute("height", window.innerHeight);
canvas.setAttribute("style", "position: absolute; x:0; y:0;");
document.body.appendChild(canvas);

//Then you can draw a point at (10,10) like this:

var ctx = canvas.getContext("2d");
ctx.fillRect(10,10,1,1);

此外,您可以使用 ImageData 操作屏幕上的所有像素。

https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Canvas_tutorial/Drawing_shapes

原文由 jhuni 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题