From 2a753599977c77c7e1cf0223b74814a85a002812 Mon Sep 17 00:00:00 2001 From: Ladd Hoffman Date: Fri, 21 Jun 2024 11:02:48 -0500 Subject: [PATCH] draw path of square --- draw.js | 73 ++++++++++++++++++++++++++++++++++++++++++++----------- test.html | 6 ++--- 2 files changed, 61 insertions(+), 18 deletions(-) diff --git a/draw.js b/draw.js index 2109a8d..7a3b28a 100644 --- a/draw.js +++ b/draw.js @@ -9,34 +9,46 @@ class Drawing { this.stopButton.onclick = () => this.stop(); this.stopButton.innerHTML = "Stop"; this.canvas = document.createElement('canvas'); - this.canvas.width = 200; - this.canvas.height = 200; buttonsDiv.appendChild(this.startButton); buttonsDiv.appendChild(this.stopButton); div.appendChild(buttonsDiv); div.appendChild(this.canvas); document.body.appendChild(div); this.ctx = this.canvas.getContext('2d'); - this.offset = {x: 0, y: 0}; this.sequence = []; this.t = 0; this.rt = 0; this.dt = 0; this.points = {}; this.stopped = true; + this.frame = [[-50, -50], [150, 150]]; + this.scale = 1.0; + this.canvas.width = (this.frame[1][0] - this.frame[0][0]) * this.scale; + this.canvas.height = (this.frame[1][1] - this.frame[0][1]) * this.scale; + this.speed = 1.0; } pixel([x, y]) { return [ - this.offset.x + x, - this.canvas.height - this.offset.y - y + (x - this.frame[0][0]) * this.scale, + this.canvas.height - (y - this.frame[0][1]) * this.scale ]; } + + setSpeed(speed) { + this.speed = speed; + } - setOffset([x, y]) { - this.sequence.push(() => { - this.offset = {x, y}; - }); + setFrame([x1, y1], [x2, y2]) { + this.frame = [[x1, y1], [x2, y2]]; + this.canvas.width = (this.frame[1][0] - this.frame[0][0]) * this.scale; + this.canvas.height = (this.frame[1][1] - this.frame[0][1]) * this.scale; + } + + setScale(zoom) { + this.scale = zoom; + this.canvas.width = (this.frame[1][0] - this.frame[0][0]) * this.scale; + this.canvas.height = (this.frame[1][1] - this.frame[0][1]) * this.scale; } setStroke(style, width) { @@ -72,8 +84,10 @@ class Drawing { }); } - oscillatingValue(x1, x2, period) { - return x1 + (x2 - x1) * Math.sin(2*Math.PI*this.t/period); + oscillatingValue(x1, x2, period, initialPhase = 0) { + const center = (x1 + x2) / 2; + const amplitude = (x2 - x1) / 2; + return center + amplitude * Math.sin(2*Math.PI*this.t/period + initialPhase); } oscillatingPoint([x1, y1], [x2, y2], period) { @@ -83,16 +97,47 @@ class Drawing { } square(p) { + let history = []; + let maxAge = 2500; + let dashLength = 5; + let distance = 0; + let width = 10; + let height = 10; this.sequence.push(() => { const [x, y] = this.getPoint(p); - this.ctx.fillRect(...this.pixel([x - 5, y - 5]), 10, 10); + this.ctx.fillRect(...this.pixel([x - width / 2, y + height / 2]), width * this.scale, height * this.scale); + let ds = 0; + if (history.length) { + const dx = x - history[history.length - 1].p[0]; + const dy = y - history[history.length - 1].p[1]; + ds = Math.sqrt(dx**2 + dy**2); + } + distance += ds; + history.push({t: this.t, p: [x, y], distance}); + const oldest = history.findIndex(({t}) => this.t - t <= maxAge); + if (oldest >= 0) { + history = history.slice(oldest); + } + this.ctx.strokeStyle = 'black'; + this.ctx.lineWidth = 1; + this.ctx.beginPath(); + this.ctx.moveTo(...this.pixel(history[0].p)); + for (let i = 1; i < history.length; i++) { + if (Math.floor(history[i].distance / dashLength) % 2) { + this.ctx.moveTo(...this.pixel(history[i].p)); + } else { + this.ctx.lineTo(...this.pixel(history[i].p)); + } + } + this.ctx.stroke(); }) } circle(p) { + const radius = 5; this.sequence.push(() => { this.ctx.beginPath(); - this.ctx.ellipse(...this.pixel(this.getPoint(p)), 5, 5, 0, 0, 2*Math.PI); + this.ctx.ellipse(...this.pixel(this.getPoint(p)), radius * this.scale, radius * this.scale, 0, 0, 2*Math.PI); this.ctx.fill(); }) } @@ -132,7 +177,7 @@ class Drawing { this.dt = 0; this.elideInterval = false; } else { - this.dt = rt - this.rt + elapsed; + this.dt = (rt - this.rt + elapsed) * this.speed; } this.t += this.dt; this.rt = rt; diff --git a/test.html b/test.html index c5b21d0..8660544 100644 --- a/test.html +++ b/test.html @@ -10,17 +10,15 @@