我想以二维方式绘制点(每个点都有x和y坐标)。我想知道是否有一个库或项目可以做到这一点,以便我不必从头开始构建。
使用Canvas标签是实现此目的的最佳方式。以下是创建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/zh-CN/docs/Web/Guide/HTML/Canvas_tutorial/Drawing_shapes
ctx.fillRect(10,10,10,10);
。 - kkron最简单的方法:
point(30,30)
这段 JavaScript 代码使用 p5js,完整示例:
HTML:
<html>
<head>
<script src="p5.min.js"></script>
<script src="sketch.js"></script>
</head>
<body style="margin: 0px;">
<main>
</main>
</body>
</html>
sketch.js:
function preload() {
}
function setup() {
createCanvas(window.innerWidth, window.innerHeight);
}
function draw() {
background(0);
stroke('white');
strokeWeight(10);
point(window.innerWidth/2,window.innerHeight/2);
}
我认为这个对你来说非常贴切:
返回x和y数组中的值,方便在图形和运动中使用
<canvas>
还是SVG? - James