简体   繁体   English

如何在 JavaScript Canvas 中绘制坐标

[英]How to draw coordinates in JavaScript Canvas

I want to draw polynomials like 4x^2+3x+4 with JavaScript and Canvas.我想用 JavaScript 和 Canvas 绘制像 4x^2+3x+4 这样的多项式。 For this I wrote a function in JavaScript that calculates these values of this polynomials and return 2000 x/y coordinates in Interval [-20, +20].为此,我在 JavaScript 中写了一个 function,它计算了这个多项式的这些值,并在区间 [-20, +20] 中返回 2000 x/y 坐标。 How i can draw this coordinates in Canvas?我如何在 Canvas 中绘制这个坐标?

Thank you谢谢

Something like the following code may help you.类似以下代码的内容可能会对您有所帮助。 Within it, a canvas is created, the width and height is set to 40 (to simulate -20 to 20), then the points, placed in an array, are combined into Path2Dpath data .在其中,创建了一个 canvas,宽度和高度设置为 40(模拟 -20 到 20),然后将放置在数组中的点组合成Path2D路径数据 20 is added to the x coordinates, because a canvas displays only coordinates on the positive plane, and the y coordinates are made negative and 40 is added to them because [0, 0] on a canvas is the top left, and positive y coordinates go down instead of up.将 20 添加到 x 坐标,因为 canvas 仅显示正平面上的坐标,并且将 y 坐标设为负数并添加 40,因为 canvas 上的 [0, 0] 是左上角,正 y 坐标go 向下而不是向上。

 document.addEventListener('DOMContentLoaded', () => { var canvas = document.createElement('canvas'); var context = canvas.getContext('2d'); canvas.width = 40; canvas.height = 40; var points = []; for(var i = -20; i <= 20; i+= 0.01) { points.push(i+20, -(4*i**2+3*i+4)+40); } var path = new Path2D('M '+points.shift()+' '+points.shift()+' L '+points.join(' ')); context.stroke(path); document.body.append(canvas); });

I would recommend, however, using an SVG instead -- if possible.但是,我建议使用 SVG 代替 - 如果可能的话。 It's a very similar idea, but it may produce a higher quality result.这是一个非常相似的想法,但它可能会产生更高质量的结果。 In this example, an svg element and a path element are created, the SVG's viewBox is set to -20 to 20 on both axis, then the same path data in the previous example is added to the path's 'd' attribute.在此示例中,创建了一个 svg 元素和一个路径元素,SVG 的 viewBox 在两个轴上都设置为 -20 到 20,然后将上一个示例中的相同路径数据添加到路径的 'd' 属性中。 X and Y aren't changed as much because the SVG's viewBox was changed, but Y is still made negative -- for the same reason as the canvas solution. X 和 Y 并没有因为 SVG 的 viewBox 发生变化而发生太大变化,但 Y 仍然是负数——原因与 canvas 解决方案相同。

 document.addEventListener('DOMContentLoaded', () => { var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); var path = document.createElementNS('http://www.w3.org/2000/svg', 'path'); var points = []; for(var i = -20; i <= 20; i+= 0.01) { points.push(i, -(4*i**2+3*i+4)); } svg.setAttribute('viewBox', '-20 -20 40 40'); path.setAttribute('d', 'M '+points.shift()+' '+points.shift()+' L '+points.join(' ')); path.setAttribute('fill', 'transparent'); path.setAttribute('stroke', 'black'); svg.append(path); document.body.append(svg); });

You might have stored your coordinates in an array like the following: [[x, y], [x, y]].您可能已将坐标存储在如下数组中:[[x, y], [x, y]]。 If so, this solution will work if array.flat() is used to convert the array to [x, y, x, y].如果是这样,如果使用array.flat()将数组转换为 [x, y, x, y],则此解决方案将起作用。

I may not have properly explained these code snippets, but hopefully this answer is useful to you.我可能没有正确解释这些代码片段,但希望这个答案对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM