Skip to content
This repository was archived by the owner on Nov 25, 2020. It is now read-only.

Commit 4e8c328

Browse files
committed
Reactive state bugfixes
1 parent c2f8084 commit 4e8c328

File tree

7 files changed

+105
-229
lines changed

7 files changed

+105
-229
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Cloud CNC
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

package.json

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vue-gcode-viewer",
33
"version": "1.2.0",
4-
"main": "dist/vue-gcode-viewer.js",
4+
"main": "dist/vue-gcode-viewer.common.js",
55
"scripts": {
66
"serve": "vue-cli-service serve src/demo/main.js",
77
"build": "vue-cli-service build --target lib --name vue-gcode-viewer src/main.js",
@@ -11,6 +11,25 @@
1111
"test:unit": "vue-cli-service test:unit",
1212
"lint": "vue-cli-service lint"
1313
},
14+
"repository": {
15+
"type": "git",
16+
"url": "git+https://github.com/Cloud-CNC/vue-gcode-viewer.git"
17+
},
18+
"keywords": [
19+
"vue",
20+
"gcode",
21+
"gcode-viewer",
22+
"vue-gcode-viewer",
23+
"gcode-visualizer",
24+
"gcode-parser",
25+
"cloud-cnc"
26+
],
27+
"author": "Cloud CNC",
28+
"license": "MIT",
29+
"bugs": {
30+
"url": "https://github.com/Cloud-CNC/vue-gcode-viewer/issues"
31+
},
32+
"homepage": "https://github.com/Cloud-CNC/vue-gcode-viewer#readme",
1433
"files": [
1534
"dist/*",
1635
"src/*"

src/assets/scene.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const state = {
2424
canvas: null,
2525
controls: null,
2626
destroyed: false,
27+
object: null,
28+
plane: null,
2729
renderer: null,
2830
scene: null
2931
};
@@ -54,29 +56,32 @@ const resize = () =>
5456
*/
5557
const setup = async (canvas, bed, theme, gcode, position, rotation, scale) =>
5658
{
59+
//Canvas
60+
state.canvas = canvas;
61+
5762
//Renderer
5863
state.renderer = new WebGLRenderer({antialias: true});
5964
state.renderer.setPixelRatio(window.devicePixelRatio);
6065
state.renderer.setSize(
61-
canvas.clientWidth,
62-
canvas.clientHeight
66+
state.canvas.clientWidth,
67+
state.canvas.clientHeight
6368
);
6469
state.renderer.encoding = GammaEncoding;
6570
state.renderer.outputEncoding = GammaEncoding;
6671
state.renderer.shadowMap.enabled = true;
67-
canvas.appendChild(state.renderer.domElement);
72+
state.canvas.appendChild(state.renderer.domElement);
6873

6974
//Camera
70-
const camera = new PerspectiveCamera(
75+
state.camera = new PerspectiveCamera(
7176
50,
72-
canvas.clientWidth / canvas.clientHeight,
77+
state.canvas.clientWidth / state.canvas.clientHeight,
7378
0.1,
7479
200
7580
);
76-
camera.position.set(0, bed.X / 2, -bed.Y);
81+
state.camera.position.set(0, bed.X / 2, -bed.Y);
7782

7883
//Orbit controls
79-
state.controls = new OrbitControls(camera, canvas);
84+
state.controls = new OrbitControls(state.camera, state.canvas);
8085
state.controls.rotateSpeed = 0.7;
8186
state.controls.minDistance = 1;
8287
state.controls.maxDistance = 100;
@@ -93,21 +98,21 @@ const setup = async (canvas, bed, theme, gcode, position, rotation, scale) =>
9398
state.scene.background = new Color(theme.backgroundColor);
9499

95100
//Plane
96-
const plane = new Mesh(
101+
state.plane = new Mesh(
97102
new PlaneBufferGeometry(),
98103
new MeshBasicMaterial({color: new Color(theme.bedColor)})
99104
);
100-
plane.rotation.x = -Math.PI / 2;
101-
plane.scale.set(bed.X, bed.Y, 1);
102-
state.scene.add(plane);
105+
state.plane.rotation.x = -Math.PI / 2;
106+
state.plane.scale.set(bed.X, bed.Y, 1);
107+
state.scene.add(state.plane);
103108

104109
//GCODE
105110
if (gcode != null)
106111
{
107-
const object = await utils.update.gcode(gcode, theme, state.scene);
108-
utils.update.position(object, position);
109-
utils.update.rotation(object, rotation);
110-
utils.update.scale(object, scale);
112+
await utils.update.gcode(gcode, theme, state.scene);
113+
utils.update.position(position);
114+
utils.update.rotation(rotation);
115+
utils.update.scale(scale);
111116
}
112117

113118
//Subscribe to resize event
@@ -120,7 +125,7 @@ const setup = async (canvas, bed, theme, gcode, position, rotation, scale) =>
120125
{
121126
state.controls.update();
122127
requestAnimationFrame(animate);
123-
state.renderer.render(state.scene, camera);
128+
state.renderer.render(state.scene, state.camera);
124129
}
125130
};
126131
animate();
@@ -169,4 +174,4 @@ const teardown = () =>
169174
};
170175

171176
//Export
172-
export {setup, teardown};
177+
export {setup, teardown, state};

src/assets/utils.js

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
//Imports
6+
import {state} from '@/assets/scene';
67
import GCodeParser from '@/assets/gcode-parser';
78

89
/**
@@ -12,20 +13,19 @@ const update = {
1213
/**
1314
* @function Update bed
1415
*/
15-
bed: (plane, bed) =>
16+
bed: bed =>
1617
{
1718
const {X, Y} = bed;
18-
plane.scale.set(X, Y, 1);
19+
state.plane.scale.set(X, Y, 1);
1920
},
2021

2122
/**
2223
* @function Update GCODE
2324
* @param {String} raw Raw GCODE
2425
* @param {Object} theme Theme
25-
* @param {Scene} scene ThreeJS scene
26-
* @returns {Promise<Object>}
26+
* @returns {Promise<void>}
2727
*/
28-
gcode: async (raw, theme, scene) =>
28+
gcode: async (raw, theme) =>
2929
{
3030
const parser = new GCodeParser(
3131
theme.extrusionColor,
@@ -35,65 +35,64 @@ const update = {
3535
const object = await parser.parse(raw);
3636

3737
//Add to scene
38-
scene.add(object.extrusion);
39-
scene.add(object.path);
38+
state.scene.add(object.extrusion);
39+
state.scene.add(object.path);
4040

41-
//Return for later manipulation
42-
return object;
41+
//Save for later manipulation
42+
state.object = object;
43+
44+
window.test = () =>
45+
{
46+
return state;
47+
};
4348
},
4449

4550
/**
4651
* @function Update object position
47-
* @param {Object} object Object
4852
* @param {Object} position Position
4953
*/
50-
position: (object, position) =>
54+
position: position =>
5155
{
5256
const {X, Y, Z} = position;
53-
object.extrusion.position.set(X, Y, Z);
54-
object.path.position.set(X, Y, Z);
57+
state.object.extrusion.position.set(X, Y, Z);
58+
state.object.path.position.set(X, Y, Z);
5559
},
5660

5761
/**
5862
* @function Update object rotation
59-
* @param {Object} object Object
6063
* @param {Object} rotation Rotation
6164
*/
62-
rotation: (object, rotation) =>
65+
rotation: rotation =>
6366
{
6467
let {X, Y, Z} = rotation;
6568
X *= Math.PI / 180;
6669
Y *= Math.PI / 180;
6770
Z *= Math.PI / 180;
68-
object.extrusion.rotation.set(X, Y, Z);
69-
object.path.rotation.set(X, Y, Z);
71+
state.object.extrusion.rotation.set(X, Y, Z);
72+
state.object.path.rotation.set(X, Y, Z);
7073
},
7174

7275
/**
7376
* @function Update object scale
74-
* @param {Object} object Object
7577
* @param {Object} scale Scale
7678
*/
77-
scale: (object, scale) =>
79+
scale: scale =>
7880
{
7981
const {X, Y, Z} = scale;
80-
object.extrusion.scale.set(Z, X, Y);
81-
object.path.scale.set(Z, X, Y);
82+
state.object.extrusion.scale.set(Z, X, Y);
83+
state.object.path.scale.set(Z, X, Y);
8284
},
8385

8486
/**
8587
* @function Update scene theme
86-
* @param {Object} object Object
8788
* @param {Object} theme Theme
88-
* @param {PlaneGeometry} plane ThreeJS plane
89-
* @param {Scene} scene Scene
9089
*/
91-
theme: (object, theme, plane, scene) =>
90+
theme: theme =>
9291
{
93-
object.extrusion.material.color.set(theme.extrusionColor);
94-
object.path.material.color.set(theme.pathColor);
95-
plane.material.color.set(theme.bedColor);
96-
scene.background.set(theme.backgroundColor);
92+
state.object.extrusion.material.color.set(theme.extrusionColor);
93+
state.object.path.material.color.set(theme.pathColor);
94+
state.plane.material.color.set(theme.bedColor);
95+
state.scene.background.set(theme.backgroundColor);
9796
}
9897
};
9998

src/gcode-viewer.vue

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,65 +53,53 @@ export default {
5353
type: Object
5454
}
5555
},
56-
data: () => ({
57-
camera: null,
58-
controls: null,
59-
scene: null,
60-
plane: null,
61-
object: {
62-
extrusion: null,
63-
path: null
64-
},
65-
renderer: null,
66-
destroyed: false
67-
}),
6856
watch: {
6957
//Update plane
7058
bed: {
7159
deep: true,
7260
handler: function ()
7361
{
74-
utils.update.bed(this.plane, this.bed);
62+
utils.update.bed(this.bed);
7563
}
7664
},
7765
//Update model
78-
gcode: function (raw)
66+
gcode: async function (raw)
7967
{
80-
utils.update.gcode(raw, this.theme, this.scene);
81-
utils.update.position(this.object, this.position);
82-
utils.update.rotation(this.object, this.rotation);
83-
utils.update.scale(this.object, this.scale);
68+
await utils.update.gcode(raw, this.theme);
69+
utils.update.position(this.position);
70+
utils.update.rotation(this.rotation);
71+
utils.update.scale(this.scale);
8472
},
8573
//Update position
8674
position: {
8775
deep: true,
8876
handler: function (value)
8977
{
90-
utils.update.position(this.object, value);
78+
utils.update.position(value);
9179
}
9280
},
9381
//Update rotation
9482
rotation: {
9583
deep: true,
9684
handler: function (value)
9785
{
98-
utils.update.rotation(this.object, value);
86+
utils.update.rotation(value);
9987
}
10088
},
10189
//Update scale
10290
scale: {
10391
deep: true,
10492
handler: function (value)
10593
{
106-
utils.update.scale(this.object, value);
94+
utils.update.scale(value);
10795
}
10896
},
10997
//Update theme
11098
theme: {
11199
deep: true,
112100
handler: function (value)
113101
{
114-
utils.update.theme(this.object, value, this.plane, this.scene);
102+
utils.update.theme(value);
115103
}
116104
}
117105
},
@@ -130,7 +118,7 @@ export default {
130118

131119
<style>
132120
#canvas {
133-
position: fixed;
121+
position: absolute;
134122
width: 100%;
135123
height: 100%;
136124
}

0 commit comments

Comments
 (0)