运行 ❯
获取您
自己的
网站
×
更改方向
保存代码
更改主题,深色/浅色
转到 Spaces
<!DOCTYPE html> <html> <body> <canvas id="myCanvas" width="400px" height="400px" style="width:100%;max-width:400px;border:1px solid black"></canvas> <script> // Create a Plotter let myPlotter = new XYPlotter("myCanvas"); myPlotter.transformXY(); // Plot a Line myPlotter.plotLine(0, 0, myPlotter.xMax, myPlotter.yMax, "red"); // Plotter Object function XYPlotter(id) { this.canvas = document.getElementById(id); this.ctx = this.canvas.getContext("2d"); this.xMin = 0; this.yMin = 0; this.xMax = this.canvas.width; this.yMax = this.canvas.height; // Transform XY Function this.transformXY = function() { this.ctx.transform(1, 0, 0, -1, 0, this.canvas.height) } // Plot a Line Function this.plotLine = function(x0, y0, x, y, color) { this.ctx.moveTo(x0, y0); this.ctx.lineTo(x, y); this.ctx.strokeStyle = color; this.ctx.stroke(); } } // End Plotter Object </script> </body> </html>