Skip to content

Commit 5609350

Browse files
authored
feat(examples): converted to use standard JS imports / exports
1 parent 89a3e4b commit 5609350

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+321
-31706
lines changed

packages/examples/core/booleans/basicBooleans.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,9 @@
88
* @licence MIT License
99
*/
1010

11-
const jscad = require('@jscad/modeling')
12-
const { cube, sphere } = jscad.primitives
13-
const { translate } = jscad.transforms
14-
const { colorize } = jscad.colors
15-
const { union, subtract, intersect } = jscad.booleans
11+
import { cube, sphere, translate, colorize, union, subtract, intersect } from '@jscad/modeling'
1612

17-
const main = () => {
13+
export const main = () => {
1814
const aCube = colorize([1, 0, 0], translate([-4.5, 0, 0], cube()))
1915
const aSphere = colorize([0, 1, 0], translate([-5.2, -0.8, 0.8], sphere({ segments: 32 })))
2016

@@ -29,5 +25,3 @@ const main = () => {
2925
translate([9, 0, 0], aIntersection)
3026
]
3127
}
32-
33-
module.exports = { main }

packages/examples/core/colors/basicColors.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@
88
* @licence MIT License
99
*/
1010

11-
const jscad = require('@jscad/modeling')
12-
const { colorize, hslToRgb, colorNameToRgb, hexToRgb, hsvToRgb } = jscad.colors
13-
const { cuboid, sphere } = jscad.primitives
14-
const { translate } = jscad.transforms
11+
import { colorize, hslToRgb, colorNameToRgb, hexToRgb, hsvToRgb, cuboid, sphere, translate } from '@jscad/modeling'
1512

16-
const main = () => {
13+
export const main = () => {
1714
// the color() function applies a color (rgb, or rgba) to the given object
1815
const simple = colorize([0, 1, 0, 0.8], cuboid())
1916

@@ -51,5 +48,3 @@ const main = () => {
5148
fromHsv
5249
]
5350
}
54-
55-
module.exports = { main }

packages/examples/core/colors/colorCube.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
* @licence MIT License
99
*/
1010

11-
const jscad = require('@jscad/modeling')
12-
const { colorize, hslToRgb, hsvToRgb } = jscad.colors
13-
const { cuboid } = jscad.primitives
14-
const { translate } = jscad.transforms
11+
import { colorize, hslToRgb, hsvToRgb, cuboid, translate } from '@jscad/modeling'
1512

1613
const getTranslation = (x, y, z, steps) => {
1714
const spacing = 4
@@ -33,7 +30,7 @@ const getColor = (a, b, c, method) => {
3330
* @param {String} params.method - The spectrum function to use: 'rgb'|'hsv'|'hsl'
3431
* @returns {[geometry]}
3532
*/
36-
const main = (params) => {
33+
export const main = (params) => {
3734
const o = []
3835
const rows = 8
3936
for (let ix = 0; ix <= rows; ix++) {
@@ -48,7 +45,7 @@ const main = (params) => {
4845
return o
4946
}
5047

51-
const getParameterDefinitions = () => [
48+
export const getParameterDefinitions = () => [
5249
{
5350
name: 'method',
5451
type: 'choice',
@@ -58,5 +55,3 @@ const getParameterDefinitions = () => [
5855
initial: 'hsl'
5956
}
6057
]
61-
62-
module.exports = { main, getParameterDefinitions }

packages/examples/core/colors/transparency.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
* @licence MIT License
99
*/
1010

11-
const { colorize, hslToRgb, colorNameToRgb } = require('@jscad/modeling').colors
12-
const { cuboid, cylinder } = require('@jscad/modeling').primitives
13-
const { translate } = require('@jscad/modeling').transforms
11+
import { colorize, hslToRgb, colorNameToRgb, cuboid, cylinder, translate } from '@jscad/modeling'
1412

15-
const main = () => {
13+
export const main = () => {
1614
const shapes = []
1715
for (let i = 7; i >= 0; i--) {
1816
// reverse order for seeing through all cylinders (see http://www.opengl.org/wiki/Transparency_Sorting)
@@ -29,5 +27,3 @@ const main = () => {
2927
)
3028
return shapes
3129
}
32-
33-
module.exports = { main }

packages/examples/core/curves/bezier/extrudeAlongPath.js

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,10 @@
88
* @licence MIT License
99
*/
1010

11-
const jscad = require('@jscad/modeling')
12-
const { colors, geometries, maths, extrusions } = jscad
11+
import { colorize, geom2, vec3, mat4, extrudeFromSlices, slice } from '@jscad/modeling'
12+
import { cuboid, circle, bezier, translate } from '@jscad/modeling'
1313

14-
const { cuboid, circle } = jscad.primitives
15-
const { translate } = jscad.transforms
16-
const { slice } = jscad.extrusions
17-
const { bezier } = jscad.curves
18-
19-
const main = () => [
14+
export const main = () => [
2015
box4x4([-8, -4, 2], [1, 0, 0]),
2116
box4x4([8, 4, 12], [0, 1, 0]),
2217
tube([
@@ -38,45 +33,45 @@ const tube = (bezierControlPoints) => {
3833
// Create the initial slice
3934
const circ = circle({ radius: 1, segments: 32 })
4035
const l = bezierControlPoints.length - 1
41-
const circPoints = geometries.geom2.toPoints(circ)
42-
let tubeSlice = slice.fromPoints(circPoints)
36+
const circPoints = geom2.toPoints(circ)
37+
let tubeSlice = slice.fromVertices(circPoints)
4338

4439
// Rotate it close to the direction we are going in. Rotation gets funky around 180˚
45-
const bezierDelta = maths.vec3.clone([
40+
const bezierDelta = vec3.clone([
4641
bezierControlPoints[l][0] - bezierControlPoints[0][0],
4742
bezierControlPoints[l][1] - bezierControlPoints[0][1],
4843
bezierControlPoints[l][2] - bezierControlPoints[0][2]
4944
])
50-
tubeSlice = slice.transform(rotationMatrixFromVectors(maths.vec3.clone([0, 0, 1]), bezierDelta), tubeSlice)
45+
tubeSlice = slice.transform(rotationMatrixFromVectors(vec3.clone([0, 0, 1]), bezierDelta), tubeSlice)
5146

5247
// Create the bezier function
5348
const tubeCurve = bezier.create(bezierControlPoints)
5449

5550
// ...and extrude.
56-
return extrusions.extrudeFromSlices({
51+
return extrudeFromSlices({
5752
numberOfSlices: 60,
5853
capStart: true,
5954
capEnd: true,
6055
callback: function (progress, count, base) {
6156
const positionArray = bezier.valueAt(progress, tubeCurve)
6257
const tangentArray = bezier.tangentAt(progress, tubeCurve)
63-
const rotationMatrix = rotationMatrixFromVectors(bezierDelta, maths.vec3.clone(tangentArray))
64-
const translationMatrix = maths.mat4.fromTranslation(maths.mat4.create(), positionArray)
65-
return slice.transform(maths.mat4.multiply(translationMatrix, translationMatrix, rotationMatrix), base)
58+
const rotationMatrix = rotationMatrixFromVectors(bezierDelta, vec3.clone(tangentArray))
59+
const translationMatrix = mat4.fromTranslation(mat4.create(), positionArray)
60+
return slice.transform(mat4.multiply(translationMatrix, translationMatrix, rotationMatrix), base)
6661
}
6762
}, tubeSlice)
6863
}
6964

7065
const rotationMatrixFromVectors = (srcVector, targetVector) => {
7166
// From https://gist.github.com/kevinmoran/b45980723e53edeb8a5a43c49f134724
72-
srcVector = maths.vec3.normalize(maths.vec3.create(), srcVector)
73-
targetVector = maths.vec3.normalize(maths.vec3.create(), targetVector)
67+
srcVector = vec3.normalize(vec3.create(), srcVector)
68+
targetVector = vec3.normalize(vec3.create(), targetVector)
7469

75-
const axis = maths.vec3.cross(maths.vec3.create(), targetVector, srcVector)
76-
const cosA = maths.vec3.dot(targetVector, srcVector)
70+
const axis = vec3.cross(vec3.create(), targetVector, srcVector)
71+
const cosA = vec3.dot(targetVector, srcVector)
7772
const k = 1 / (1 + cosA)
7873

79-
return maths.mat4.fromValues(
74+
return mat4.fromValues(
8075
(axis[0] * axis[0] * k) + cosA, (axis[1] * axis[0] * k) - axis[2], (axis[2] * axis[0] * k) + axis[1], 0,
8176
(axis[0] * axis[1] * k) + axis[2], (axis[1] * axis[1] * k) + cosA, (axis[2] * axis[1] * k) - axis[0], 0,
8277
(axis[0] * axis[2] * k) - axis[1], (axis[1] * axis[2] * k) + axis[0], (axis[2] * axis[2] * k) + cosA, 0,
@@ -86,7 +81,5 @@ const rotationMatrixFromVectors = (srcVector, targetVector) => {
8681

8782
const box4x4 = (translation, color) => {
8883
const b = cuboid({ size: [4, 4, 4] })
89-
return colors.colorize(color, translate(translation, b))
84+
return colorize(color, translate(translation, b))
9085
}
91-
92-
module.exports = { main }

packages/examples/core/curves/bezier/simpleExtrude.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,25 @@
88
* @licence MIT License
99
*/
1010

11-
const jscad = require('@jscad/modeling')
12-
const { maths, extrusions } = jscad
13-
const { bezier } = jscad.curves
14-
const { slice } = extrusions
11+
import { mat4, extrudeFromSlices, slice, bezier } from '@jscad/modeling'
1512

16-
const main = () => [
13+
export const main = () => [
1714
extrudeWobble(30)
1815
]
1916

2017
const extrudeWobble = (height) => {
21-
const squareSlice = slice.fromPoints([[10, 10], [-10, 10], [-10, -10], [10, -10]])
18+
const squareSlice = slice.fromVertices([[10, 10], [-10, 10], [-10, -10], [10, -10]])
2219

2320
const xCurve = bezier.create([1, 2, 0.4, 1])
2421
const yCurve = bezier.create([1, 2, 0.5])
2522

26-
return extrusions.extrudeFromSlices({
23+
return extrudeFromSlices({
2724
numberOfSlices: 20,
2825
capStart: true,
2926
capEnd: true,
3027
callback: function (progress, count, base) {
31-
let newslice = slice.transform(maths.mat4.fromTranslation(maths.mat4.create(), [0, 0, height * progress]), base)
32-
newslice = slice.transform(maths.mat4.fromScaling(maths.mat4.create(), [
28+
let newslice = slice.transform(mat4.fromTranslation(mat4.create(), [0, 0, height * progress]), base)
29+
newslice = slice.transform(mat4.fromScaling(mat4.create(), [
3330
bezier.valueAt(progress, xCurve),
3431
bezier.valueAt(progress, yCurve),
3532
1
@@ -38,5 +35,3 @@ const extrudeWobble = (height) => {
3835
}
3936
}, squareSlice)
4037
}
41-
42-
module.exports = { main }

packages/examples/core/extrusions/basicExtrusions.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,23 @@
88
* @licence MIT License
99
*/
1010

11-
const jscad = require('@jscad/modeling')
12-
const { line, polygon, star } = jscad.primitives
13-
const { extrudeRectangular, extrudeLinear, extrudeRotate } = jscad.extrusions
14-
const { translate } = jscad.transforms
15-
const { offset } = jscad.offsets
11+
import { line, polygon, star, extrudeLinear, extrudeRotate, translate, offset, TAU } from '@jscad/modeling'
1612

17-
const main = () => {
13+
export const main = () => {
1814
const shapes = []
1915
const aLine = line([[0, 0], [0, 5], [2, 8], [5, 9]])
2016
shapes.push(translate([-17, 0, 0], aLine))
2117

22-
const aRectangularExtrude = extrudeRectangular({ size: 1, height: 1 }, aLine)
23-
shapes.push(translate([-12, 0, 0], aRectangularExtrude))
24-
2518
const anExpandedExtrude = extrudeLinear({ height: 1 }, offset({ delta: 1, corners: 'round', segments: 32 }, aLine))
2619
shapes.push(translate([-7, 0, 0], anExpandedExtrude))
2720

2821
const poly = polygon({ points: [[-1, -1], [3, -1], [3.5, 2], [2, 1], [1, 2], [0, 1], [-1, 2]] })
29-
const extrudedPoly = extrudeLinear({ height: 5, twistAngle: Math.PI / 4, twistSteps: 10 }, poly)
22+
const extrudedPoly = extrudeLinear({ height: 5, twistAngle: TAU / 8, twistSteps: 10 }, poly)
3023
shapes.push(translate([-1, 0, 0], extrudedPoly))
3124

3225
const starPoly = translate([3, 0, 0], star())
33-
const extrudedStar = extrudeRotate({ segments: 32, startAngle: 0, angle: (Math.PI * 0.75), overflow: 'cap' }, starPoly)
26+
const extrudedStar = extrudeRotate({ segments: 32, startAngle: 0, angle: (TAU / 2 * 0.75), overflow: 'cap' }, starPoly)
3427
shapes.push(translate([9, 0, 0], extrudedStar))
3528

3629
return shapes
3730
}
38-
39-
module.exports = { main }

packages/examples/core/extrusions/extrudeFromSlices.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@
88
* @licence MIT License
99
*/
1010

11-
const { circle } = require('@jscad/modeling').primitives
12-
const { geom2 } = require('@jscad/modeling').geometries
13-
const { extrudeFromSlices, slice } = require('@jscad/modeling').extrusions
14-
const { mat4 } = require('@jscad/modeling').maths
11+
import { circle, geom2, extrudeFromSlices, slice, mat4, TAU } from '@jscad/modeling'
1512

16-
const main = () => {
13+
export const main = () => {
1714
// demonstrates manipulating the original base through translation and scale to build a 3D geometry
1815
const jigglySquare = (height) => {
1916
let squareWithHole = geom2.create(
@@ -28,11 +25,11 @@ const main = () => {
2825
[[-8.0, 8.0], [8.0, 8.0]]
2926
]
3027
)
31-
squareWithHole = slice.fromSides(geom2.toSides(squareWithHole))
28+
squareWithHole = slice.fromGeom2(squareWithHole)
3229
return extrudeFromSlices({
3330
numberOfSlices: 32,
3431
callback: (progress, count, base) => {
35-
const scaleFactor = 1 + (0.03 * Math.cos(3 * Math.PI * progress))
32+
const scaleFactor = 1 + (0.03 * Math.cos(3 * TAU / 2 * progress))
3633
const scaleMatrix = mat4.fromScaling(mat4.create(), [scaleFactor, 2 - scaleFactor, 1])
3734
const transformMatrix = mat4.fromTranslation(mat4.create(), [0, 0, progress * height])
3835
return slice.transform(mat4.multiply(mat4.create(), scaleMatrix, transformMatrix), base)
@@ -50,7 +47,7 @@ const main = () => {
5047
numberOfSlices: 6,
5148
callback: (progress, count, base) => {
5249
const newPolygon = circle({ radius: 2 + (5 * progress), segments: 4 + (count * count) })
53-
let newSlice = slice.fromSides(geom2.toSides(newPolygon))
50+
let newSlice = slice.fromGeom2(newPolygon)
5451
newSlice = slice.transform(mat4.fromTranslation(mat4.create(), [0, 0, progress * height]), newSlice)
5552
return newSlice
5653
}
@@ -63,5 +60,3 @@ const main = () => {
6360
squareToCircleExtrusion(10)
6461
]
6562
}
66-
67-
module.exports = { main }

packages/examples/core/extrusions/nutsAndBolts.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,8 @@
88
* @licence MIT License
99
*/
1010

11-
const jscad = require('@jscad/modeling')
12-
const { cylinder } = jscad.primitives
13-
const { subtract, union } = jscad.booleans
14-
const { colorize } = jscad.colors
15-
const { extrudeFromSlices, slice } = jscad.extrusions
16-
const { translate } = jscad.transforms
11+
import { cylinder, extrudeFromSlices, slice, TAU } from '@jscad/modeling'
12+
import { subtract, union, colorize, translate } from '@jscad/modeling'
1713

1814
const options = {
1915
hexWidth: 10,
@@ -26,7 +22,7 @@ const options = {
2622
segments: 32
2723
}
2824

29-
const main = () => [
25+
export const main = () => [
3026
colorize([0.9, 0.6, 0.2], bolt(options)),
3127
colorize([0.4, 0.4, 0.4], translate([30, 0, 0], nut(options)))
3228
]
@@ -61,18 +57,18 @@ const threads = (options) => {
6157
// generate each slice manually
6258
const points = []
6359
for (let i = 0; i < segments; i++) {
64-
const pointAngle = Math.PI * 2 * i / segments
65-
const threadAngle = (2 * Math.PI * revolutions * progress) % (Math.PI * 2)
60+
const pointAngle = TAU * i / segments
61+
const threadAngle = (TAU * revolutions * progress) % TAU
6662

6763
// define the shape of the threads
68-
const phase = angleDiff(threadAngle, pointAngle) / Math.PI
64+
const phase = angleDiff(threadAngle, pointAngle) / TAU / 2
6965
const radius = lerp(innerRadius, outerRadius, 1.4 * phase - 0.2)
7066

7167
const x = radius * Math.cos(pointAngle)
7268
const y = radius * Math.sin(pointAngle)
7369
points.push([x, y, threadLength * progress])
7470
}
75-
return slice.fromPoints(points)
71+
return slice.fromVertices(points)
7672
}
7773
}, {})
7874
}
@@ -81,8 +77,6 @@ const threads = (options) => {
8177
const lerp = (a, b, t) => Math.max(a, Math.min(b, a + (b - a) * t))
8278

8379
const angleDiff = (angle1, angle2) => {
84-
const diff = Math.abs((angle1 - angle2) % (Math.PI * 2))
85-
return diff > Math.PI ? Math.PI * 2 - diff : diff
80+
const diff = Math.abs((angle1 - angle2) % TAU)
81+
return diff > (TAU / 2) ? TAU - diff : diff
8682
}
87-
88-
module.exports = { main }

packages/examples/core/hulls/hull2D.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@
88
* @licence MIT License
99
*/
1010

11-
const jscad = require('@jscad/modeling')
12-
const { circle, rectangle } = jscad.primitives
13-
const { translate } = jscad.transforms
14-
const { hull, hullChain } = jscad.hulls
11+
import { circle, rectangle, translate, hull, hullChain } from '@jscad/modeling'
1512

16-
const getParameterDefinitions = () => [
13+
export const getParameterDefinitions = () => [
1714
{ name: 'doHull', type: 'radio', caption: 'Show:', values: ['shapes', 'hull', 'chain'], captions: ['Original Shapes', 'Hull', 'Hull Chain'], initial: 'shapes' }
1815
]
1916

20-
const main = (params) => {
17+
export const main = (params) => {
2118
const shapes = [
2219
translate([15, 0, 0], circle({ radius: 2, segments: 16 })),
2320
translate([8, 6, 0], circle({ radius: 3.5, segments: 16 })),
@@ -32,5 +29,3 @@ const main = (params) => {
3229
return shapes
3330
}
3431
}
35-
36-
module.exports = { main, getParameterDefinitions }

0 commit comments

Comments
 (0)