spiral example ported!
This commit is contained in:
parent
fc12f05e3a
commit
440d531f0e
@ -15,7 +15,7 @@ export class Drawable {
|
||||
|
||||
_register() {
|
||||
if (this._parent) {
|
||||
this._parent.push(this);
|
||||
this._parent._register(this);
|
||||
}
|
||||
artworld.register(this);
|
||||
}
|
||||
@ -26,6 +26,12 @@ export class Drawable {
|
||||
return obj;
|
||||
}
|
||||
|
||||
draw() {
|
||||
// TODO: log/raise?
|
||||
}
|
||||
|
||||
// setters
|
||||
|
||||
fill(color) {
|
||||
this._fill = color;
|
||||
return this;
|
||||
@ -73,7 +79,10 @@ export class Drawable {
|
||||
// getters /////////////////////////////
|
||||
|
||||
get worldPos() {
|
||||
return this._parent ? this._parent.worldPos.add(this._pos) : this._pos;
|
||||
// offset from parent if needed
|
||||
return this._parent
|
||||
? this._parent.worldPos.add(this._posVec)
|
||||
: this._posVec;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,7 @@
|
||||
export { artworld } from "./world.js";
|
||||
export { Color } from "./color.js";
|
||||
export { Vector2 } from "./math.js";
|
||||
export { Vector2, degToRad, radToDeg } from "./math.js";
|
||||
export { Line } from "./shapes.js";
|
||||
export { Group } from "./drawable.js";
|
||||
export { Random } from "./random.js";
|
||||
console.log("here");
|
||||
|
@ -1,4 +1,12 @@
|
||||
export const TO_RADIANS = 180 / Math.PI;
|
||||
const PI_180 = Math.PI / 180;
|
||||
|
||||
export function degToRad(degrees) {
|
||||
return degrees * PI_180;
|
||||
}
|
||||
|
||||
export function radToDeg(rad) {
|
||||
return rad / PI_1i0;
|
||||
}
|
||||
|
||||
export class Vector2 {
|
||||
constructor(x, y) {
|
||||
@ -7,15 +15,15 @@ export class Vector2 {
|
||||
}
|
||||
|
||||
add(other) {
|
||||
return Vector2(this.x + other.x, this.y + other.y);
|
||||
return new Vector2(this.x + other.x, this.y + other.y);
|
||||
}
|
||||
|
||||
sub(other) {
|
||||
return Vector2(this.x - other.x, this.y - other.y);
|
||||
return new Vector2(this.x - other.x, this.y - other.y);
|
||||
}
|
||||
|
||||
scale(s) {
|
||||
return Vector2(s * this.x, s * this.y);
|
||||
return new Vector2(s * this.x, s * this.y);
|
||||
}
|
||||
|
||||
static random(x, y) {
|
||||
@ -28,10 +36,10 @@ export class Vector2 {
|
||||
if (y === undefined) {
|
||||
y = x;
|
||||
}
|
||||
return Vector2(x * cos(theta), y * sin(theta));
|
||||
return new Vector2(x * cos(theta), y * sin(theta));
|
||||
}
|
||||
|
||||
static polar(mag, angle) {
|
||||
return Vector2(mag * Math.cos(angle), mag * Math.sin(angle));
|
||||
return new Vector2(mag * Math.cos(angle), mag * Math.sin(angle));
|
||||
}
|
||||
}
|
||||
|
5
artworld/random.js
Normal file
5
artworld/random.js
Normal file
@ -0,0 +1,5 @@
|
||||
export class Random {
|
||||
static chance(odds) {
|
||||
return Math.random() < odds;
|
||||
}
|
||||
}
|
@ -10,7 +10,7 @@ export class Line extends Drawable {
|
||||
draw() {
|
||||
artworld.setStrokeColor(this._stroke);
|
||||
artworld.setStrokeWeight(this._strokeWeight);
|
||||
artworld.drawLine(this._posVec, this._offsetVec);
|
||||
artworld.drawLine(this.worldPos, this.worldPos.add(this._offsetVec));
|
||||
}
|
||||
|
||||
to(vec) {
|
||||
@ -18,10 +18,6 @@ export class Line extends Drawable {
|
||||
return this;
|
||||
}
|
||||
|
||||
polar(mag, angle) {
|
||||
return this.to(Vector2.polar(mag, angle));
|
||||
}
|
||||
|
||||
angle(angle) {
|
||||
let mag = this._offsetVec.magnitude;
|
||||
return this.to(Vector2.polar(mag, angle));
|
||||
|
@ -25,11 +25,13 @@ export class World {
|
||||
}
|
||||
|
||||
setStrokeColor(color) {
|
||||
this.ctx.strokeStyle = color.toStr();
|
||||
if (color && color.toStr) this.ctx.strokeStyle = color.toStr();
|
||||
else this.ctx.strokeStyle = color;
|
||||
}
|
||||
|
||||
setFillColor(color) {
|
||||
this.ctx.fillStyle = color.toStr();
|
||||
if (color && color.toStr) this.ctx.fillStyle = color.toStr();
|
||||
else this.ctx.fillStyle = color;
|
||||
}
|
||||
|
||||
fillRect(rect) {
|
||||
|
58
examples/spiral.html
Normal file
58
examples/spiral.html
Normal file
@ -0,0 +1,58 @@
|
||||
<html>
|
||||
<head> </head>
|
||||
<body>
|
||||
<canvas id="mainCanvas" width="800" height="800"></canvas>
|
||||
<script type="module">
|
||||
import {
|
||||
artworld,
|
||||
Color,
|
||||
Group,
|
||||
Line,
|
||||
Random,
|
||||
Vector2,
|
||||
degToRad,
|
||||
} from "../artworld/index.js";
|
||||
artworld.bindCanvas("mainCanvas");
|
||||
window.artworld = artworld;
|
||||
|
||||
function* spirals() {
|
||||
while (true) {
|
||||
let g = new Group();
|
||||
for (let d = 0; d < 180; d += 10) {
|
||||
if (Random.chance(0.9)) {
|
||||
new Line(g).to(Vector2.polar(190 - d, degToRad(d)));
|
||||
}
|
||||
}
|
||||
yield g;
|
||||
}
|
||||
}
|
||||
|
||||
function makeGrid(iterable, rows, cols, opts) {
|
||||
opts = opts || {};
|
||||
let width = opts.width || 50;
|
||||
let height = opts.height || 50;
|
||||
let xOff = opts.xOff || 0;
|
||||
let yOff = opts.yOff || 0;
|
||||
for (let c = 0; c < cols; c++) {
|
||||
for (let r = 0; r < rows; r++) {
|
||||
let result = iterable.next().value;
|
||||
if (result === undefined) {
|
||||
return;
|
||||
} else {
|
||||
result.pos(new Vector2(width * c + xOff, height * r + yOff));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
makeGrid(spirals(), 4, 3, {
|
||||
width: 220,
|
||||
height: 120,
|
||||
xOff: 80,
|
||||
yOff: 10,
|
||||
});
|
||||
|
||||
artworld.draw();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user