Skip to content

Commit e748b54

Browse files
committed
✨ Add web build
1 parent 74c63e4 commit e748b54

File tree

12 files changed

+5047
-715
lines changed

12 files changed

+5047
-715
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.idea/
2+
dist/
23
example/dist/main.js
34
node_modules/
45
src/components/generated/

build.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { writeFile } = require("fs").promises;
22
const { promise: clean } = require("delete");
33

44
const component = (pencil, props) => `import Pencil from "pencil.js";
5-
import Component from "../Component.vue";
5+
import Component from "../Component.js";
66
77
export default {
88
extends: Component,

package-lock.json

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

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
"version": "0.4.0",
44
"description": "Pencil.js ❤️ Vue - Build reactive 2D graphics scene in your Vue project",
55
"main": "src/index.js",
6+
"unpkg": "dist/main.js",
7+
"jsdelivr": "dist/main.js",
68
"scripts": {
79
"build": "node ./build.js",
8-
"prepublishOnly": "npm run build"
10+
"web": "webpack -p",
11+
"prepublishOnly": "npm run build && npm run web"
912
},
1013
"eslintConfig": {
1114
"extends": "@gmartigny/eslint-config"
@@ -30,6 +33,9 @@
3033
"devDependencies": {
3134
"@gmartigny/eslint-config": "^1.4.2",
3235
"delete": "^1.1.0",
33-
"eslint": "^6.8.0"
36+
"eslint": "^6.8.0",
37+
"pencil.js": "^1.16.0",
38+
"webpack": "^4.42.1",
39+
"webpack-cli": "^3.3.11"
3440
}
3541
}

readme.md

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ Pencil.js ❤️ Vue - Build reactive 2D graphics scene in your Vue project
1111

1212
## Usage
1313

14-
You have 2 choices.
14+
You have 3 choices:
1515
1. Install `vue-pencil.js` globally and use it wherever you want in your `Vuejs` app.
16-
2. Or, import only components you need in your `Vuejs` components.
16+
2. Import only components you need in your `Vuejs` components.
17+
3. Use a direct `<script>` tag to include from a CDN.
1718

1819
### 1. Global install
1920

@@ -30,7 +31,7 @@ new Vue({
3031
el: "#app",
3132
template: `
3233
<p-scene>
33-
<p-circle :radius="100" />
34+
<p-circle :radius="100" :position="[100, 100]" />
3435
</p-scene>
3536
`,
3637
});
@@ -41,7 +42,7 @@ new Vue({
4142
```vue
4243
<template>
4344
<PScene>
44-
<PCircle :radius="100" />
45+
<PCircle :radius="100" :position="[100, 100]" />
4546
</PScene>
4647
</template>
4748
@@ -58,6 +59,35 @@ import { PScene, PCircle } from "vue-pencil.js";
5859
</script>
5960
```
6061

62+
### 3. Direct `<script>` tag
63+
64+
```html
65+
<!DOCTYPE html>
66+
<html lang="en">
67+
<head>
68+
<meta charset="UTF-8">
69+
<title>Example</title>
70+
</head>
71+
<body>
72+
<div id="app"></div>
73+
<!-- Vue script tag-->
74+
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
75+
<!-- vue-pencil.js script tag-->
76+
<script src="https://unpkg.com/vue-pencil.js"></script>
77+
<script>
78+
new Vue({
79+
el: "#app",
80+
template: `
81+
<p-scene>
82+
<p-circle :radius="100" :position="[100, 100]" />
83+
</p-scene>
84+
`,
85+
});
86+
</script>
87+
</body>
88+
</html>
89+
```
90+
6191

6292
## Example
6393

src/components/Component.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import PContainer from "./Container.js";
2+
3+
export default {
4+
extends: PContainer,
5+
props: ["draggable"],
6+
mounted () {
7+
if (!this.$pencil) {
8+
throw new Error("Component should not be instantiated by itself.")
9+
}
10+
11+
if (this.draggable !== undefined) {
12+
this.$pencil.draggable();
13+
}
14+
},
15+
};

src/components/Component.vue

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

src/components/Container.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { MouseEvent } from "pencil.js";
2+
3+
export default {
4+
template: "<div><slot/></div>",
5+
props: ["position", "options"],
6+
watch: {
7+
position () {
8+
this.$pencil.position.set(this.position);
9+
},
10+
options () {
11+
this.$pencil.setOptions(this.options);
12+
},
13+
},
14+
computed: {
15+
frameCount () {
16+
return this.$pencil.frameCount;
17+
},
18+
},
19+
mounted () {
20+
if (this.$parent.$pencil) {
21+
this.$parent.$pencil.add(this.$pencil);
22+
}
23+
24+
[
25+
...Object.values(MouseEvent.events),
26+
...Object.values(this.$pencil.constructor.events),
27+
].forEach((event) => {
28+
this.$pencil.on(event, (...args) => this.$emit(event, ...args));
29+
});
30+
},
31+
updated () {
32+
console.log("Update", this.$vnode.tag);
33+
},
34+
destroyed () {
35+
if (this.$parent.$pencil) {
36+
this.$parent.$pencil.remove(this.$pencil);
37+
}
38+
},
39+
};

src/components/Container.vue

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

src/components/Scene.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { MouseEvent, Scene } from "pencil.js";
2+
3+
const mirroredProps = ["cursorPosition", "fps", "width", "height", "center", "size", "frameCount"];
4+
const computed = mirroredProps.reduce((obj, prop) => {
5+
obj[prop] = function () {
6+
return this.$pencil[prop];
7+
};
8+
return obj;
9+
}, {});
10+
11+
const mirroredFunction = ["setImageSmoothing", "getRandomPosition", "getImageData", "toImage"];
12+
const methods = mirroredFunction.reduce((obj, prop) => {
13+
obj[prop] = function (...args) {
14+
return this.$pencil[prop](...args);
15+
};
16+
return obj;
17+
}, {});
18+
19+
export default {
20+
template: "<div><slot/></div>",
21+
props: ["options"],
22+
watch: {
23+
options () {
24+
this.$pencil.setOptions(this.options);
25+
},
26+
},
27+
computed,
28+
methods,
29+
beforeMount () {
30+
// Temporary scene to host children
31+
const container = document.createElement("div");
32+
this.$pencil = new Scene(container, this.options);
33+
},
34+
mounted () {
35+
const oldScene = this.$pencil;
36+
const container = this.$el.parentNode;
37+
this.$pencil = new Scene(container, this.options);
38+
container.replaceChild(this.$pencil.ctx.canvas, this.$el);
39+
40+
// Transfer children from old to new scene
41+
oldScene.children.forEach(child => this.$pencil.add(child));
42+
43+
[
44+
...Object.values(MouseEvent.events),
45+
...Object.values(this.$pencil.constructor.events),
46+
].forEach((event) => {
47+
this.$pencil.on(event, (...args) => this.$emit(event, ...args));
48+
});
49+
50+
this.$pencil.startLoop();
51+
},
52+
};

0 commit comments

Comments
 (0)