diff --git a/bundle.js b/bundle.js index bcd612c..a2296d7 100644 --- a/bundle.js +++ b/bundle.js @@ -132,6 +132,14 @@ class Doodler { this.ctx.bezierCurveTo(b.x, b.y, c.x, c.y, d.x, d.y); this.ctx.stroke(); } + drawRotated(origin, angle, cb) { + this.ctx.save(); + this.ctx.translate(origin.x, origin.y); + this.ctx.rotate(angle); + this.ctx.translate(-origin.x, -origin.y); + cb(); + this.ctx.restore(); + } setStyle(style) { const ctx = this.ctx; ctx.fillStyle = style?.color || style?.fillColor || 'black'; @@ -373,6 +381,7 @@ init({ height: 400 }); const movingVector = new Vector(100, 300); +let angleMultiplier = 0; doodler.createLayer(()=>{ doodler.line(new Vector(100, 100), new Vector(200, 200)); doodler.dot(new Vector(300, 300)); @@ -386,5 +395,10 @@ doodler.createLayer(()=>{ weight: 5 }); doodler.drawBezier(new Vector(100, 150), movingVector, new Vector(150, 300), new Vector(100, 250)); + let rotatedOrigin = new Vector(200, 200); + doodler.drawRotated(rotatedOrigin, Math.PI * angleMultiplier, ()=>{ + doodler.drawCenteredSquare(rotatedOrigin, 30); + }); movingVector.set((movingVector.x + 1) % 400, movingVector.y); + angleMultiplier += .001; }); diff --git a/canvas.ts b/canvas.ts index 891d853..db4f2a5 100644 --- a/canvas.ts +++ b/canvas.ts @@ -163,6 +163,15 @@ export class Doodler { this.ctx.stroke(); } + drawRotated(origin: Vector, angle: number, cb: () => void){ + this.ctx.save(); + this.ctx.translate(origin.x, origin.y); + this.ctx.rotate(angle); + this.ctx.translate(-origin.x, -origin.y); + cb(); + this.ctx.restore(); + } + setStyle(style?: IStyle) { const ctx = this.ctx; ctx.fillStyle = style?.color || style?.fillColor || 'black'; diff --git a/main.ts b/main.ts index 73ed360..108be21 100644 --- a/main.ts +++ b/main.ts @@ -1,4 +1,5 @@ import { Doodler } from "./canvas.ts"; +import { Constants } from "./geometry/constants.ts"; /// import { Vector, initializeDoodler } from './mod.ts' @@ -11,6 +12,7 @@ initializeDoodler({ // let doodler = window['doodler']; const movingVector = new Vector(100, 300); +let angleMultiplier = 0; doodler.createLayer(() => { doodler.line(new Vector(100, 100), new Vector(200, 200)) @@ -22,5 +24,11 @@ doodler.createLayer(() => { doodler.drawCenteredSquare(new Vector(200, 200), 40, { color: 'purple', weight: 5 }) doodler.drawBezier(new Vector(100, 150), movingVector, new Vector(150, 300), new Vector(100, 250)) + let rotatedOrigin = new Vector(200,200) + doodler.drawRotated(rotatedOrigin, Math.PI*angleMultiplier, () => { + doodler.drawCenteredSquare(rotatedOrigin, 30) + }) + movingVector.set((movingVector.x + 1) % 400, movingVector.y); + angleMultiplier += .001; });