Skip to content

Commit 72c26c7

Browse files
Merge pull request #35 from Demonstrandum/devel
Devel
2 parents bbd8745 + fc27733 commit 72c26c7

14 files changed

+51
-31
lines changed

.nowignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package.json

Dockerfile

Lines changed: 0 additions & 6 deletions
This file was deleted.

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ A friendlier way interact with the canvas.
55
jsdelivr CDN (use this to import):
66
- Canvas
77
```
8-
https://cdn.jsdelivr.net/gh/Demonstrandum/BasicCanvas@v1.0.6/lib/BasicCanvas.js
8+
https://cdn.jsdelivr.net/gh/Demonstrandum/BasicCanvas@v1.0.8/lib/BasicCanvas.js
99
```
1010
- Shapes
1111
```
12-
https://cdn.jsdelivr.net/gh/Demonstrandum/BasicCanvas@v1.0.6/lib/BasicShapes.js
12+
https://cdn.jsdelivr.net/gh/Demonstrandum/BasicCanvas@v1.0.8/lib/BasicShapes.js
1313
```
1414
- DOM
1515
```
16-
https://cdn.jsdelivr.net/gh/Demonstrandum/BasicCanvas@v1.0.6/lib/BasicDOM.js
16+
https://cdn.jsdelivr.net/gh/Demonstrandum/BasicCanvas@v1.0.8/lib/BasicDOM.js
1717
```
1818

1919
TODO: Instructions on usage, for now look at the example files (and/or source files), still a small project.

example/double_pendulum.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ css`
7474
`;
7575

7676
const BG = HEX('#dfcbeb');
77-
sketch.fill = HEX('#000000aa');
7877
sketch.stroke_cap = 'round';
7978

8079
let coord_1 = Point(0, L_1);
@@ -83,6 +82,7 @@ const trail = [];
8382

8483
sketch.loop(() => {
8584
sketch.background(BG);
85+
sketch.fill = 'transparent';
8686
sketch.shape('trail', shape => {
8787
sketch.stroke_weight = 1;
8888
let alpha = 1;
@@ -97,6 +97,7 @@ sketch.loop(() => {
9797
}
9898

9999
sketch.stroke_weight = 2.5;
100+
sketch.fill = HEX('#000000aa');
100101
sketch.stroke = HEX('#000');
101102
sketch.shape('origin', ellipse(Point(0, 0), 3));
102103
sketch.shape('harnes', line(Point(0, 0), coord_1));

example/night_sky.coffee

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ sketch.loop (frame) ->
1818
alpha = 0
1919
for location in stars
2020
size = alpha / 40
21-
shape = sketch.render star location, 4, 5 + size, 5
22-
shape.fill RGBA 255, 255, 130, alpha
21+
sketch.fill = RGBA 255, 255, 130, alpha
22+
sketch.render star location, 4, 5 + size, 5
2323

2424
alpha += 255 / stars.length
2525

2626
if stars.length > 100
2727
stars.shift()
2828

29+
sketch.fill = '#fff'
2930
sketch.font = 'bold 100px Georgia'
3031
sketch.text 'Heilige', P 71, 148
3132
sketch.text 'Nacht', P 60, 230

example/night_sky.js

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/polygons.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ canvas.dimensions(400, 400);
88
canvas.translate(canvas.width / 2, canvas.height / 2);
99
canvas.scale(40, 40);
1010

11-
canvas.fill = BC.HEX('#ccc');
1211
canvas.stroke = BC.HEX('#111');
1312
canvas.font = '12px monospace';
1413

example/sketchbook.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ canvas.scale(10, 10);
88
canvas.stroke = BC.HEX`#a9f`;
99
canvas.stroke_weight = 20;
1010
canvas.stroke_cap = 'round';
11-
canvas.fill = BC.HEX('#fafafa');
1211
canvas.font = '12px monospace';
1312

1413
let vertices = [];
@@ -23,12 +22,14 @@ BC.mouse_up(() => {
2322
vertices = [];
2423
}, canvas);
2524

25+
canvas.fill = BC.HEX('#fafafa');
2626
canvas.background();
2727
canvas.loop(() => {
2828
if (drawing) {
2929
vertices.push(canvas.mouse);
3030
}
3131

32+
canvas.fill = 'transparent';
3233
canvas.shape('path', shape => {
3334
for (const vertex of vertices) {
3435
shape.vertex(vertex);

example/wiggler.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const death = new Audio('https://freesound.org/data/previews/190/190843_329661
99

1010
BC.css`body, html { overflow: hidden; }`;
1111
canvas.dimensions(window.innerWidth, window.innerHeight);
12+
canvas.stroke_cap = 'round';
1213

1314
class Snake {
1415
constructor(name) {

lib/BasicCanvas.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ class Shape {
337337

338338
this.vertices = [];
339339
this.center = Point(0, 0);
340+
this.hollow = false;
340341
}
341342

342343
flesh() {
@@ -354,7 +355,7 @@ class Shape {
354355
stroke = 'transparent';
355356
}
356357
const c = this.canvas.context;
357-
c.fillStyle = fill.toString();
358+
c.fillStyle = (this.hollow) ? 'transparent' : fill.toString();
358359
c.strokeStyle = stroke.toString();
359360
c.lineWidth = stroke_weight;
360361
c.lineCap = stroke_cap;
@@ -421,11 +422,19 @@ class Shape {
421422
temp_color = this.canvas.fill;
422423
}
423424

425+
if (this.hollow
426+
|| temp_color.valueOf() === 'transparent'
427+
|| (this.vertices.length < 3 && !this.primitive)) {
428+
return;
429+
}
430+
424431
if (this.primitive === null) {
425-
const c = this.canvas.context;
426-
c.moveTo(...this.vertices[0]);
427-
for (const vertex of this.vertices.slice(1)) {
428-
c.lineTo(...vertex);
432+
if (this.vertices.length > 0) {
433+
const c = this.canvas.context;
434+
c.moveTo(...this.vertices[0]);
435+
for (const vertex of this.vertices.slice(1)) {
436+
c.lineTo(...vertex);
437+
}
429438
}
430439
} else {
431440
this.primitive();
@@ -456,7 +465,7 @@ class Canvas {
456465
this.data = this.image_data.data;
457466

458467
// Main API properties.
459-
this.fill = RGB(255, 255, 255);
468+
this.fill = RGBA(255, 255, 255, 0);
460469
this.stroke = RGB(0, 0, 0);
461470
this._stroke_weight = 1;
462471
this.stroke_cap = 'butt';
@@ -620,15 +629,19 @@ class Canvas {
620629
_name = name;
621630
}
622631

632+
const SHAPE = new Shape(_name, this);
623633
this.shapes[_name] = {
624634
draw: _construction,
625-
shape: new Shape(_name, this)
635+
shape: SHAPE
626636
};
627637
this.context.beginPath();
628-
_construction(this.shapes[_name].shape);
638+
_construction(SHAPE);
629639
this.context.closePath();
630-
this.shapes[_name].shape.fill(this.fill);
631-
return this.shapes[_name].shape;
640+
641+
if (this.fill !== 'transparent' && SHAPE.primitive === null) {
642+
SHAPE.fill();
643+
}
644+
return SHAPE;
632645
}
633646

634647
render(...args) {

0 commit comments

Comments
 (0)