Skip to content

Commit

Permalink
Merge pull request #35 from Demonstrandum/devel
Browse files Browse the repository at this point in the history
Devel
  • Loading branch information
Demonstrandum authored Mar 4, 2019
2 parents bbd8745 + fc27733 commit 72c26c7
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 31 deletions.
1 change: 1 addition & 0 deletions .nowignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package.json
6 changes: 0 additions & 6 deletions Dockerfile

This file was deleted.

6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ A friendlier way interact with the canvas.
jsdelivr CDN (use this to import):
- Canvas
```
https://cdn.jsdelivr.net/gh/Demonstrandum/BasicCanvas@v1.0.6/lib/BasicCanvas.js
https://cdn.jsdelivr.net/gh/Demonstrandum/BasicCanvas@v1.0.8/lib/BasicCanvas.js
```
- Shapes
```
https://cdn.jsdelivr.net/gh/Demonstrandum/BasicCanvas@v1.0.6/lib/BasicShapes.js
https://cdn.jsdelivr.net/gh/Demonstrandum/BasicCanvas@v1.0.8/lib/BasicShapes.js
```
- DOM
```
https://cdn.jsdelivr.net/gh/Demonstrandum/BasicCanvas@v1.0.6/lib/BasicDOM.js
https://cdn.jsdelivr.net/gh/Demonstrandum/BasicCanvas@v1.0.8/lib/BasicDOM.js
```

TODO: Instructions on usage, for now look at the example files (and/or source files), still a small project.
Expand Down
3 changes: 2 additions & 1 deletion example/double_pendulum.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ css`
`;

const BG = HEX('#dfcbeb');
sketch.fill = HEX('#000000aa');
sketch.stroke_cap = 'round';

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

sketch.loop(() => {
sketch.background(BG);
sketch.fill = 'transparent';
sketch.shape('trail', shape => {
sketch.stroke_weight = 1;
let alpha = 1;
Expand All @@ -97,6 +97,7 @@ sketch.loop(() => {
}

sketch.stroke_weight = 2.5;
sketch.fill = HEX('#000000aa');
sketch.stroke = HEX('#000');
sketch.shape('origin', ellipse(Point(0, 0), 3));
sketch.shape('harnes', line(Point(0, 0), coord_1));
Expand Down
5 changes: 3 additions & 2 deletions example/night_sky.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ sketch.loop (frame) ->
alpha = 0
for location in stars
size = alpha / 40
shape = sketch.render star location, 4, 5 + size, 5
shape.fill RGBA 255, 255, 130, alpha
sketch.fill = RGBA 255, 255, 130, alpha
sketch.render star location, 4, 5 + size, 5

alpha += 255 / stars.length

if stars.length > 100
stars.shift()

sketch.fill = '#fff'
sketch.font = 'bold 100px Georgia'
sketch.text 'Heilige', P 71, 148
sketch.text 'Nacht', P 60, 230
9 changes: 5 additions & 4 deletions example/night_sky.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion example/polygons.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ canvas.dimensions(400, 400);
canvas.translate(canvas.width / 2, canvas.height / 2);
canvas.scale(40, 40);

canvas.fill = BC.HEX('#ccc');
canvas.stroke = BC.HEX('#111');
canvas.font = '12px monospace';

Expand Down
3 changes: 2 additions & 1 deletion example/sketchbook.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ canvas.scale(10, 10);
canvas.stroke = BC.HEX`#a9f`;
canvas.stroke_weight = 20;
canvas.stroke_cap = 'round';
canvas.fill = BC.HEX('#fafafa');
canvas.font = '12px monospace';

let vertices = [];
Expand All @@ -23,12 +22,14 @@ BC.mouse_up(() => {
vertices = [];
}, canvas);

canvas.fill = BC.HEX('#fafafa');
canvas.background();
canvas.loop(() => {
if (drawing) {
vertices.push(canvas.mouse);
}

canvas.fill = 'transparent';
canvas.shape('path', shape => {
for (const vertex of vertices) {
shape.vertex(vertex);
Expand Down
1 change: 1 addition & 0 deletions example/wiggler.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const death = new Audio('https://freesound.org/data/previews/190/190843_329661

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

class Snake {
constructor(name) {
Expand Down
33 changes: 23 additions & 10 deletions lib/BasicCanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ class Shape {

this.vertices = [];
this.center = Point(0, 0);
this.hollow = false;
}

flesh() {
Expand All @@ -354,7 +355,7 @@ class Shape {
stroke = 'transparent';
}
const c = this.canvas.context;
c.fillStyle = fill.toString();
c.fillStyle = (this.hollow) ? 'transparent' : fill.toString();
c.strokeStyle = stroke.toString();
c.lineWidth = stroke_weight;
c.lineCap = stroke_cap;
Expand Down Expand Up @@ -421,11 +422,19 @@ class Shape {
temp_color = this.canvas.fill;
}

if (this.hollow
|| temp_color.valueOf() === 'transparent'
|| (this.vertices.length < 3 && !this.primitive)) {
return;
}

if (this.primitive === null) {
const c = this.canvas.context;
c.moveTo(...this.vertices[0]);
for (const vertex of this.vertices.slice(1)) {
c.lineTo(...vertex);
if (this.vertices.length > 0) {
const c = this.canvas.context;
c.moveTo(...this.vertices[0]);
for (const vertex of this.vertices.slice(1)) {
c.lineTo(...vertex);
}
}
} else {
this.primitive();
Expand Down Expand Up @@ -456,7 +465,7 @@ class Canvas {
this.data = this.image_data.data;

// Main API properties.
this.fill = RGB(255, 255, 255);
this.fill = RGBA(255, 255, 255, 0);
this.stroke = RGB(0, 0, 0);
this._stroke_weight = 1;
this.stroke_cap = 'butt';
Expand Down Expand Up @@ -620,15 +629,19 @@ class Canvas {
_name = name;
}

const SHAPE = new Shape(_name, this);
this.shapes[_name] = {
draw: _construction,
shape: new Shape(_name, this)
shape: SHAPE
};
this.context.beginPath();
_construction(this.shapes[_name].shape);
_construction(SHAPE);
this.context.closePath();
this.shapes[_name].shape.fill(this.fill);
return this.shapes[_name].shape;

if (this.fill !== 'transparent' && SHAPE.primitive === null) {
SHAPE.fill();
}
return SHAPE;
}

render(...args) {
Expand Down
3 changes: 3 additions & 0 deletions lib/BasicShapes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const ellipse = (point, w, h = null, fill = null, stroke = null) => shape
};

export const line = (origin, position) => shape => {
shape.hollow = true;
shape.vertex(origin);
shape.vertex(position);
};
Expand Down Expand Up @@ -50,6 +51,7 @@ export const polar_line = (mag, angle, origin = BC.Point(0, 0)) => shape => {
};

export const arrow = (mag, angle, origin = BC.Point(0, 0), headsize = 1 / 5) => shape => {
shape.hollow = true;
polar_line(mag, angle, origin)(shape);
const arrow_angle = 2.4;
const point = BC.Polar(mag, angle, origin);
Expand All @@ -60,6 +62,7 @@ export const arrow = (mag, angle, origin = BC.Point(0, 0), headsize = 1 / 5) =>
};

export const vector = (point, origin = BC.Point(0, 0), headsize = 1 / 5) => shape => {
shape.hollow = true;
line(origin, point)(shape);
const arrow_angle = 2.4;
const length = point.length(origin);
Expand Down
7 changes: 6 additions & 1 deletion now.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@
"name": "BasicCanvas",
"alias" : "canvas.knutsen.co",
"version": 2,
"builds": [{ "src": "*.py", "use": "@now/python"}]
"builds": [
{ "src": "*", "use": "@now/static" },
{ "src": "lib/*", "use": "@now/static"},
{ "src": "example/*", "use": "@now/static" }
]
}

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "basiccanvas",
"title": "BasicCanvas",
"description": "Simple JavaScript canvas abstractions.",
"version": "1.0.7",
"version": "1.0.8",
"main": "lib/BasicCanvas.js",
"homepage": "https://github.com/Demonstrandum/BasicCanvas/",
"author": {
Expand Down
2 changes: 1 addition & 1 deletion server.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/sh
echo "Go to: http://localhost:8000/example"
python3 -m http.server 8000 &> server.log
python3 -m http.server 8000

1 comment on commit 72c26c7

@vercel
Copy link

@vercel vercel bot commented on 72c26c7 Mar 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully aliased the URL https://basiccanvas-n1m7le1mt.now.sh to the following alias.

Please sign in to comment.