fragment-tools 0.1.13 → 0.1.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (184) hide show
  1. package/.prettierignore +1 -2
  2. package/.prettierrc +23 -7
  3. package/README.md +28 -9
  4. package/bin/index.js +70 -10
  5. package/package.json +14 -6
  6. package/src/cli/build.js +125 -0
  7. package/src/cli/create.js +238 -0
  8. package/src/cli/createConfig.js +82 -0
  9. package/src/cli/createFragmentFile.js +70 -0
  10. package/src/cli/getEntries.js +85 -0
  11. package/src/cli/log.js +36 -24
  12. package/src/cli/plugins/check-dependencies.js +88 -42
  13. package/src/cli/plugins/hot-shader-replacement.js +408 -0
  14. package/src/cli/plugins/hot-sketch-reload.js +21 -25
  15. package/src/cli/plugins/save.js +101 -0
  16. package/src/cli/preview.js +55 -0
  17. package/src/cli/prompts.js +260 -0
  18. package/src/cli/run.js +131 -0
  19. package/src/cli/templates/blank/index.js +33 -0
  20. package/src/cli/templates/blank/meta.json +4 -0
  21. package/src/cli/templates/default/index.js +39 -0
  22. package/src/cli/templates/default/meta.json +5 -0
  23. package/src/cli/templates/fragment-gl/index.js +37 -0
  24. package/src/cli/templates/fragment-gl/meta.json +4 -0
  25. package/src/cli/templates/p5/index.js +32 -0
  26. package/src/cli/templates/p5/meta.json +5 -0
  27. package/src/cli/templates/p5-webgl/fragment.fs +14 -0
  28. package/src/cli/templates/p5-webgl/index.js +67 -0
  29. package/src/cli/templates/p5-webgl/meta.json +5 -0
  30. package/src/cli/templates/three-fragment/fragment.fs +10 -0
  31. package/src/cli/templates/three-fragment/index.js +95 -0
  32. package/src/cli/templates/three-fragment/meta.json +5 -0
  33. package/src/cli/templates/three-orthographic/index.js +55 -0
  34. package/src/cli/templates/three-orthographic/meta.json +5 -0
  35. package/src/cli/templates/three-perspective/index.js +52 -0
  36. package/src/cli/templates/three-perspective/meta.json +5 -0
  37. package/src/cli/utils.js +70 -0
  38. package/src/cli/ws.js +87 -78
  39. package/src/client/app/App.svelte +3 -3
  40. package/src/client/app/client.js +55 -39
  41. package/src/client/app/components/IconCross.svelte +18 -18
  42. package/src/client/app/components/Init.svelte +40 -8
  43. package/src/client/app/components/KeyBinding.svelte +22 -22
  44. package/src/client/app/helpers.js +42 -0
  45. package/src/client/app/hooks.js +20 -0
  46. package/src/client/app/inputs/Input.js +9 -9
  47. package/src/client/app/inputs/Keyboard.js +13 -15
  48. package/src/client/app/inputs/MIDI.js +14 -15
  49. package/src/client/app/inputs/Mouse.js +1 -1
  50. package/src/client/app/inputs/Webcam.js +89 -88
  51. package/src/client/app/lib/canvas-recorder/CanvasRecorder.js +41 -21
  52. package/src/client/app/lib/canvas-recorder/FrameRecorder.js +7 -6
  53. package/src/client/app/lib/canvas-recorder/H264Recorder.js +45 -0
  54. package/src/client/app/lib/canvas-recorder/MP4Recorder.js +7 -9
  55. package/src/client/app/lib/canvas-recorder/WebMRecorder.js +3 -4
  56. package/src/client/app/lib/canvas-recorder/mp4.js +1649 -15
  57. package/src/client/app/lib/canvas-recorder/utils.js +33 -17
  58. package/src/client/app/lib/gl/Geometry.js +11 -8
  59. package/src/client/app/lib/gl/Program.js +38 -19
  60. package/src/client/app/lib/gl/Renderer.js +163 -156
  61. package/src/client/app/lib/gl/Texture.js +113 -85
  62. package/src/client/app/lib/gl/index.js +12 -12
  63. package/src/client/app/lib/gl/utils.js +1 -3
  64. package/src/client/app/lib/helpers/frameDebounce.js +30 -30
  65. package/src/client/app/lib/loader/index.js +10 -10
  66. package/src/client/app/lib/loader/loadImage.js +15 -15
  67. package/src/client/app/lib/loader/loadScript.js +1 -1
  68. package/src/client/app/lib/paper-sizes.js +75 -76
  69. package/src/client/app/lib/presets.js +25 -5
  70. package/src/client/app/lib/tempo/Analyser.js +18 -17
  71. package/src/client/app/lib/tempo/Range.js +15 -12
  72. package/src/client/app/lib/tempo/index.js +34 -27
  73. package/src/client/app/modules/AudioAnalyser/Range.svelte +69 -72
  74. package/src/client/app/modules/AudioAnalyser/Spectrum.svelte +20 -19
  75. package/src/client/app/modules/AudioAnalyser.svelte +52 -35
  76. package/src/client/app/modules/Console/ConsoleLine.svelte +193 -172
  77. package/src/client/app/modules/Console.svelte +76 -74
  78. package/src/client/app/modules/Exports.svelte +62 -43
  79. package/src/client/app/modules/MidiPanel.svelte +100 -101
  80. package/src/client/app/modules/Monitor.svelte +57 -57
  81. package/src/client/app/modules/Params.svelte +128 -103
  82. package/src/client/app/renderers/2DRenderer.js +3 -3
  83. package/src/client/app/renderers/FragmentRenderer.js +30 -23
  84. package/src/client/app/renderers/P5GLRenderer.js +144 -0
  85. package/src/client/app/renderers/P5Renderer.js +10 -7
  86. package/src/client/app/renderers/THREERenderer.js +136 -94
  87. package/src/client/app/stores/audioAnalysis.js +3 -4
  88. package/src/client/app/stores/console.js +9 -10
  89. package/src/client/app/stores/errors.js +1 -1
  90. package/src/client/app/stores/exports.js +36 -20
  91. package/src/client/app/stores/index.js +2 -2
  92. package/src/client/app/stores/layout.js +143 -138
  93. package/src/client/app/stores/multisampling.js +4 -4
  94. package/src/client/app/stores/props.js +76 -13
  95. package/src/client/app/stores/renderers.js +26 -15
  96. package/src/client/app/stores/rendering.js +108 -89
  97. package/src/client/app/stores/sketches.js +7 -9
  98. package/src/client/app/stores/time.js +18 -18
  99. package/src/client/app/stores/utils.js +95 -38
  100. package/src/client/app/transitions/fade.js +3 -3
  101. package/src/client/app/transitions/index.js +6 -7
  102. package/src/client/app/transitions/splitX.js +2 -2
  103. package/src/client/app/transitions/splitY.js +2 -2
  104. package/src/client/app/triggers/Keyboard.js +88 -79
  105. package/src/client/app/triggers/MIDI.js +110 -84
  106. package/src/client/app/triggers/Mouse.js +73 -65
  107. package/src/client/app/triggers/Trigger.js +59 -58
  108. package/src/client/app/triggers/index.js +7 -7
  109. package/src/client/app/triggers/shared.js +5 -5
  110. package/src/client/app/ui/Build.svelte +70 -71
  111. package/src/client/app/ui/ErrorOverlay.svelte +118 -104
  112. package/src/client/app/ui/Field.svelte +393 -258
  113. package/src/client/app/ui/FieldGroup.svelte +106 -94
  114. package/src/client/app/ui/FieldSection.svelte +127 -116
  115. package/src/client/app/ui/FieldSpace.svelte +29 -30
  116. package/src/client/app/ui/FieldTrigger.svelte +256 -244
  117. package/src/client/app/ui/FieldTriggers.svelte +46 -46
  118. package/src/client/app/ui/FloatingParams.svelte +29 -30
  119. package/src/client/app/ui/Layout.svelte +31 -32
  120. package/src/client/app/ui/LayoutColumn.svelte +4 -4
  121. package/src/client/app/ui/LayoutComponent.svelte +239 -225
  122. package/src/client/app/ui/LayoutResizer.svelte +195 -176
  123. package/src/client/app/ui/LayoutRoot.svelte +6 -6
  124. package/src/client/app/ui/LayoutRow.svelte +4 -4
  125. package/src/client/app/ui/LayoutToolbar.svelte +191 -194
  126. package/src/client/app/ui/Module.svelte +134 -135
  127. package/src/client/app/ui/ModuleHeaderAction.svelte +81 -78
  128. package/src/client/app/ui/ModuleHeaderButton.svelte +12 -12
  129. package/src/client/app/ui/ModuleHeaderSelect.svelte +47 -37
  130. package/src/client/app/ui/ModuleRenderer.svelte +26 -27
  131. package/src/client/app/ui/OutputRenderer.svelte +112 -105
  132. package/src/client/app/ui/ParamsMultisampling.svelte +96 -95
  133. package/src/client/app/ui/ParamsOutput.svelte +130 -113
  134. package/src/client/app/ui/Preview.svelte +7 -8
  135. package/src/client/app/ui/SelectChevrons.svelte +27 -15
  136. package/src/client/app/ui/SketchRenderer.svelte +780 -667
  137. package/src/client/app/ui/SketchSelect.svelte +50 -44
  138. package/src/client/app/ui/fields/ButtonInput.svelte +61 -48
  139. package/src/client/app/ui/fields/CheckboxInput.svelte +67 -61
  140. package/src/client/app/ui/fields/ColorInput.svelte +294 -238
  141. package/src/client/app/ui/fields/FieldInputRow.svelte +8 -8
  142. package/src/client/app/ui/fields/ImageInput.svelte +123 -121
  143. package/src/client/app/ui/fields/Input.svelte +100 -111
  144. package/src/client/app/ui/fields/IntervalInput.svelte +268 -0
  145. package/src/client/app/ui/fields/ListInput.svelte +96 -96
  146. package/src/client/app/ui/fields/NumberInput.svelte +120 -116
  147. package/src/client/app/ui/fields/ProgressInput.svelte +99 -73
  148. package/src/client/app/ui/fields/Select.svelte +137 -124
  149. package/src/client/app/ui/fields/TextInput.svelte +10 -11
  150. package/src/client/app/ui/fields/VectorInput.svelte +86 -82
  151. package/src/client/app/utils/canvas.utils.js +189 -208
  152. package/src/client/app/utils/color.utils.js +138 -101
  153. package/src/client/app/utils/fields.utils.js +131 -0
  154. package/src/client/app/utils/file.utils.js +209 -37
  155. package/src/client/app/utils/glsl.utils.js +2 -2
  156. package/src/client/app/utils/glslErrors.js +49 -31
  157. package/src/client/app/utils/index.js +32 -29
  158. package/src/client/app/utils/math.utils.js +14 -10
  159. package/src/client/index.html +16 -16
  160. package/src/client/main.js +4 -4
  161. package/src/client/public/css/global.css +26 -16
  162. package/src/cli/db.js +0 -17
  163. package/src/cli/index.js +0 -198
  164. package/src/cli/plugins/db.js +0 -12
  165. package/src/cli/plugins/hot-shader-reload.js +0 -86
  166. package/src/cli/plugins/screenshot.js +0 -46
  167. package/src/cli/server.js +0 -153
  168. package/src/cli/templates/2d.js +0 -15
  169. package/src/cli/templates/blank.js +0 -13
  170. package/src/cli/templates/fragment.js +0 -18
  171. package/src/cli/templates/index.js +0 -27
  172. package/src/cli/templates/p5.js +0 -13
  173. package/src/cli/templates/three-fragment.js +0 -53
  174. package/src/cli/templates/three-orthographic.js +0 -23
  175. package/src/cli/templates/three-perspective.js +0 -20
  176. package/src/client/app/lib/canvas-recorder/FFMPEGRecorder.js +0 -56
  177. package/src/client/app/utils/props.utils.js +0 -51
  178. package/src/client/public/fonts/Inter-Bold.woff2 +0 -0
  179. package/src/client/public/fonts/Inter-Italic.woff2 +0 -0
  180. package/src/client/public/fonts/Inter-Regular.woff2 +0 -0
  181. package/src/client/public/fonts/Inter-SemiBold.woff2 +0 -0
  182. package/src/client/public/js/ffmpeg.min.js +0 -2
  183. package/src/client/public/js/ffmpeg.min.js.map +0 -1
  184. /package/src/cli/templates/{fragment.fs → fragment-gl/fragment.fs} +0 -0
@@ -1,28 +1,31 @@
1
- import { isPowerOf2 } from "./utils";
1
+ import { isPowerOf2 } from './utils';
2
2
 
3
3
  const emptyPixel = new Uint8Array([0, 0, 0, 0]);
4
4
 
5
5
  let TEXTURE_ID = 0;
6
6
 
7
7
  class Texture {
8
- constructor(gl, {
9
- image,
10
- name = '',
11
- target = gl.TEXTURE_2D,
12
- type = gl.UNSIGNED_BYTE,
13
- wrapS = gl.CLAMP_TO_EDGE,
14
- wrapT = gl.CLAMP_TO_EDGE,
15
- generateMipmaps = true,
16
- format = gl.RGBA,
17
- internalFormat = format,
18
- minFilter = generateMipmaps ? gl.NEAREST_MIPMAP_LINEAR : gl.LINEAR,
19
- magFilter = gl.LINEAR,
20
- // premultiplyAlpha = false,
21
- // unpackAlignment = 4,
22
- flipY = target === gl.TEXTURE_2D ? true : false,
23
- // width = image ? image.width : null,
24
- // height = image ? image.height : null
25
- } = {}) {
8
+ constructor(
9
+ gl,
10
+ {
11
+ image,
12
+ name = '',
13
+ target = gl.TEXTURE_2D,
14
+ type = gl.UNSIGNED_BYTE,
15
+ wrapS = gl.CLAMP_TO_EDGE,
16
+ wrapT = gl.CLAMP_TO_EDGE,
17
+ generateMipmaps = true,
18
+ format = gl.RGBA,
19
+ internalFormat = format,
20
+ minFilter = generateMipmaps ? gl.NEAREST_MIPMAP_LINEAR : gl.LINEAR,
21
+ magFilter = gl.LINEAR,
22
+ // premultiplyAlpha = false,
23
+ // unpackAlignment = 4,
24
+ flipY = target === gl.TEXTURE_2D ? true : false,
25
+ // width = image ? image.width : null,
26
+ // height = image ? image.height : null
27
+ } = {},
28
+ ) {
26
29
  this.gl = gl;
27
30
  this.image = image;
28
31
  this.name = name;
@@ -42,73 +45,98 @@ class Texture {
42
45
  this.id = TEXTURE_ID++;
43
46
  }
44
47
 
45
- bind() {
46
- if (this.gl.state.textureUnits[this.gl.state.activeTextureUnit] === this.id) return;
47
-
48
- this.gl.bindTexture(this.target, this.glTexture);
49
- this.gl.state.textureUnits[this.gl.state.activeTextureUnit] = this.id;
50
- }
51
-
52
- update(textureUnit = 0) {
53
- if (this.needsUpdate || this.gl.state.textureUnits[textureUnit] !== this.id) {
54
- if (this.gl.state.activeTextureUnit !== textureUnit) {
55
- this.gl.state.activeTextureUnit = textureUnit;
56
- this.gl.activeTexture(this.gl.TEXTURE0 + textureUnit);
57
- }
58
-
59
- this.bind();
60
- }
61
-
62
- if (!this.needsUpdate) return;
63
-
64
- this.needsUpdate = false;
65
-
66
- if (this.flipY !== this.gl.state.flipY) {
67
- this.gl.pixelStorei(this.gl.UNPACK_FLIP_Y_WEBGL, this.flipY);
68
- this.gl.state.flipY = this.flipY;
69
- }
70
-
71
- if (this.image && this.generateMipmaps) {
72
- if (!isPowerOf2(this.image.width) || !isPowerOf2(this.image.height)) {
73
- this.generateMipmaps = false;
74
-
75
- this.wrapS = this.wrapT = this.gl.CLAMP_TO_EDGE;
76
- this.minFilter = this.gl.LINEAR;
77
- }
78
- }
79
-
80
- this.gl.texParameteri(this.target, this.gl.TEXTURE_WRAP_S, this.wrapS);
81
- this.gl.texParameteri(this.target, this.gl.TEXTURE_WRAP_T, this.wrapT);
82
- this.gl.texParameteri(this.target, this.gl.TEXTURE_MIN_FILTER, this.minFilter);
83
- this.gl.texParameteri(this.target, this.gl.TEXTURE_MAG_FILTER, this.magFilter);
84
-
85
- if (this.image) {
86
- this.gl.texImage2D(this.target, 0, this.internalFormat, this.format, this.type, this.image);
87
-
88
- if (this.generateMipmaps) {
89
- this.gl.generateMipmap(this.target);
90
- }
91
- } else {
92
- this.gl.texImage2D(
93
- this.target,
94
- 0,
95
- this.gl.RGBA,
96
- 1,
97
- 1,
98
- 0,
99
- this.gl.RGBA,
100
- this.gl.UNSIGNED_BYTE,
101
- emptyPixel
102
- );
103
- }
104
- }
105
-
106
- destroy() {
107
- this.gl.deleteTexture(this.glTexture);
108
- this.glTexture = null;
109
- this.gl = null;
48
+ bind() {
49
+ if (
50
+ this.gl.state.textureUnits[this.gl.state.activeTextureUnit] ===
51
+ this.id
52
+ )
53
+ return;
54
+
55
+ this.gl.bindTexture(this.target, this.glTexture);
56
+ this.gl.state.textureUnits[this.gl.state.activeTextureUnit] = this.id;
57
+ }
58
+
59
+ update(textureUnit = 0) {
60
+ if (
61
+ this.needsUpdate ||
62
+ this.gl.state.textureUnits[textureUnit] !== this.id
63
+ ) {
64
+ if (this.gl.state.activeTextureUnit !== textureUnit) {
65
+ this.gl.state.activeTextureUnit = textureUnit;
66
+ this.gl.activeTexture(this.gl.TEXTURE0 + textureUnit);
67
+ }
68
+
69
+ this.bind();
70
+ }
71
+
72
+ if (!this.needsUpdate) return;
73
+
74
+ this.needsUpdate = false;
75
+
76
+ if (this.flipY !== this.gl.state.flipY) {
77
+ this.gl.pixelStorei(this.gl.UNPACK_FLIP_Y_WEBGL, this.flipY);
78
+ this.gl.state.flipY = this.flipY;
79
+ }
80
+
81
+ if (this.image && this.generateMipmaps) {
82
+ if (
83
+ !isPowerOf2(this.image.width) ||
84
+ !isPowerOf2(this.image.height)
85
+ ) {
86
+ this.generateMipmaps = false;
87
+
88
+ this.wrapS = this.wrapT = this.gl.CLAMP_TO_EDGE;
89
+ this.minFilter = this.gl.LINEAR;
90
+ }
91
+ }
92
+
93
+ this.gl.texParameteri(this.target, this.gl.TEXTURE_WRAP_S, this.wrapS);
94
+ this.gl.texParameteri(this.target, this.gl.TEXTURE_WRAP_T, this.wrapT);
95
+ this.gl.texParameteri(
96
+ this.target,
97
+ this.gl.TEXTURE_MIN_FILTER,
98
+ this.minFilter,
99
+ );
100
+ this.gl.texParameteri(
101
+ this.target,
102
+ this.gl.TEXTURE_MAG_FILTER,
103
+ this.magFilter,
104
+ );
105
+
106
+ if (this.image) {
107
+ this.gl.texImage2D(
108
+ this.target,
109
+ 0,
110
+ this.internalFormat,
111
+ this.format,
112
+ this.type,
113
+ this.image,
114
+ );
115
+
116
+ if (this.generateMipmaps) {
117
+ this.gl.generateMipmap(this.target);
118
+ }
119
+ } else {
120
+ this.gl.texImage2D(
121
+ this.target,
122
+ 0,
123
+ this.gl.RGBA,
124
+ 1,
125
+ 1,
126
+ 0,
127
+ this.gl.RGBA,
128
+ this.gl.UNSIGNED_BYTE,
129
+ emptyPixel,
130
+ );
131
+ }
132
+ }
133
+
134
+ destroy() {
135
+ this.gl.deleteTexture(this.glTexture);
136
+ this.glTexture = null;
137
+ this.gl = null;
110
138
  this.image = null;
111
- }
139
+ }
112
140
  }
113
141
 
114
142
  export default Texture;
@@ -1,7 +1,7 @@
1
- import Geometry from "./Geometry.js";
2
- import Texture from "./Texture.js";
3
- import Program, { defaultFragment, defaultVertex } from "./Program.js";
4
- import Renderer from "./Renderer.js";
1
+ import Geometry from './Geometry.js';
2
+ import Texture from './Texture.js';
3
+ import Program, { defaultFragment, defaultVertex } from './Program.js';
4
+ import Renderer from './Renderer.js';
5
5
 
6
6
  export { Geometry, Texture, Program, Renderer };
7
7
 
@@ -58,43 +58,43 @@ export function fragment({
58
58
 
59
59
  Object.defineProperty(frag, 'shader', {
60
60
  enumerable: true,
61
- configurable: true,
61
+ configurable: true,
62
62
  get: () => {
63
63
  return _fragmentShader;
64
64
  },
65
65
  set: (v) => {
66
66
  _fragmentShader = v;
67
67
  program.fragmentShader = _fragmentShader;
68
- }
68
+ },
69
69
  });
70
70
 
71
71
  Object.defineProperty(frag, 'fragmentShader', {
72
72
  enumerable: true,
73
- configurable: true,
73
+ configurable: true,
74
74
  get: () => {
75
75
  return _fragmentShader;
76
76
  },
77
77
  set: (v) => {
78
78
  _fragmentShader = v;
79
79
  program.fragmentShader = _fragmentShader;
80
- }
80
+ },
81
81
  });
82
82
 
83
83
  Object.defineProperty(frag, 'vertexShader', {
84
84
  enumerable: true,
85
- configurable: true,
85
+ configurable: true,
86
86
  get: () => {
87
87
  return _vertexShader;
88
88
  },
89
89
  set: (v) => {
90
90
  _vertexShader = v;
91
91
  program.vertexShader = _vertexShader;
92
- }
92
+ },
93
93
  });
94
94
 
95
95
  Object.defineProperty(frag, 'uniforms', {
96
96
  enumerable: true,
97
- configurable: true,
97
+ configurable: true,
98
98
  get: () => {
99
99
  return _uniforms;
100
100
  },
@@ -102,7 +102,7 @@ export function fragment({
102
102
  _uniforms = v;
103
103
  program.uniforms = _uniforms;
104
104
  program.needsUpdate = true;
105
- }
105
+ },
106
106
  });
107
107
 
108
108
  return frag;
@@ -1,5 +1,3 @@
1
-
2
-
3
1
  export function isPowerOf2(value) {
4
- return (value & (value - 1)) === 0;
2
+ return (value & (value - 1)) === 0;
5
3
  }
@@ -1,40 +1,40 @@
1
1
  /**
2
2
  * Run a function maximum once per frame
3
- * @param {Function} fn
4
- * @param {Number} timeout
3
+ * @param {Function} fn
4
+ * @param {Number} timeout
5
5
  * @returns {Function} debounced function
6
6
  */
7
- export default function frameDebounce(fn, timeout = 100){
8
- let lastTime = performance.now();
9
- let needCall = false;
10
- let _raf;
11
- let a;
7
+ export default function frameDebounce(fn, timeout = 100) {
8
+ let lastTime = performance.now();
9
+ let needCall = false;
10
+ let _raf;
11
+ let a;
12
12
 
13
- function loop() {
14
- const now = performance.now();
13
+ function loop() {
14
+ const now = performance.now();
15
15
 
16
- if (needCall) {
17
- needCall = false;
18
- lastTime = now;
19
-
20
- fn.apply(this, a);
21
- lastTime = now;
22
- }
16
+ if (needCall) {
17
+ needCall = false;
18
+ lastTime = now;
23
19
 
24
- if (now - lastTime > timeout && _raf > -1) {
25
- cancelAnimationFrame(_raf);
26
- _raf = null;
27
- } else {
28
- _raf = requestAnimationFrame(loop);
29
- }
30
- }
20
+ fn.apply(this, a);
21
+ lastTime = now;
22
+ }
31
23
 
32
- return (...args) => {
33
- needCall = true;
34
- a = args;
24
+ if (now - lastTime > timeout && _raf > -1) {
25
+ cancelAnimationFrame(_raf);
26
+ _raf = null;
27
+ } else {
28
+ _raf = requestAnimationFrame(loop);
29
+ }
30
+ }
35
31
 
36
- if (!_raf) {
37
- loop();
38
- }
39
- };
32
+ return (...args) => {
33
+ needCall = true;
34
+ a = args;
35
+
36
+ if (!_raf) {
37
+ loop();
38
+ }
39
+ };
40
40
  }
@@ -1,20 +1,20 @@
1
- import { loadImage } from "./loadImage";
1
+ import { loadImage } from './loadImage';
2
2
 
3
3
  function getFileExtension(path) {
4
- return path.match(/[^\\/]\.([^.\\/]+)$/);
4
+ return path.match(/[^\\/]\.([^.\\/]+)$/);
5
5
  }
6
6
 
7
7
  function getLoader(extension) {
8
- if (["jpeg", "jpg", "gif", "png", "webp"].includes(extension)) {
9
- return loadImage;
10
- }
8
+ if (['jpeg', 'jpg', 'gif', 'png', 'webp'].includes(extension)) {
9
+ return loadImage;
10
+ }
11
11
 
12
- return () => {};
12
+ return () => {};
13
13
  }
14
14
 
15
15
  export function load(url, options = { id: url }) {
16
- const extension = getFileExtension(url);
17
- const loader = options.loader || getLoader(extension);
16
+ const extension = getFileExtension(url);
17
+ const loader = options.loader || getLoader(extension);
18
18
 
19
- return loader(url, options);
20
- };
19
+ return loader(url, options);
20
+ }
@@ -1,19 +1,19 @@
1
1
  export function loadImage(url, { id = url, img = new Image() } = {}) {
2
- return new Promise((resolve, reject) => {
3
- function onLoad() {
4
- img.removeEventListener('load', onLoad);
5
- resolve(img);
6
- }
2
+ return new Promise((resolve, reject) => {
3
+ function onLoad() {
4
+ img.removeEventListener('load', onLoad);
5
+ resolve(img);
6
+ }
7
7
 
8
- function onError(error) {
9
- img.removeEventListener('load', onLoad);
10
- img.removeEventListener('error', onError);
11
- reject(error);
12
- }
8
+ function onError(error) {
9
+ img.removeEventListener('load', onLoad);
10
+ img.removeEventListener('error', onError);
11
+ reject(error);
12
+ }
13
13
 
14
- img.addEventListener('load', onLoad);
15
- img.addEventListener('error', onError);
14
+ img.addEventListener('load', onLoad);
15
+ img.addEventListener('error', onError);
16
16
 
17
- img.src = url;
18
- });
19
- };
17
+ img.src = url;
18
+ });
19
+ }
@@ -6,7 +6,7 @@ export function loadScript(src) {
6
6
  };
7
7
  script.onload = () => {
8
8
  resolve();
9
- }
9
+ };
10
10
 
11
11
  script.src = src;
12
12
  document.body.appendChild(script);
@@ -4,97 +4,96 @@ const defaultUnits = 'mm';
4
4
  const data = [
5
5
  // Common Paper Sizes
6
6
  // (Mostly North-American based)
7
- [ 'postcard', 101.6, 152.4 ],
8
- [ 'poster-small', 280, 430 ],
9
- [ 'poster', 460, 610 ],
10
- [ 'poster-large', 610, 910 ],
11
- [ 'business-card', 50.8, 88.9 ],
7
+ ['postcard', 101.6, 152.4],
8
+ ['poster-small', 280, 430],
9
+ ['poster', 460, 610],
10
+ ['poster-large', 610, 910],
11
+ ['business-card', 50.8, 88.9],
12
12
 
13
13
  // Photographic Print Paper Sizes
14
- [ '2r', 64, 89 ],
15
- [ '3r', 89, 127 ],
16
- [ '4r', 102, 152 ],
17
- [ '5r', 127, 178 ], // 5″x7″
18
- [ '6r', 152, 203 ], // 6″x8″
19
- [ '8r', 203, 254 ], // 8″x10″
20
- [ '10r', 254, 305 ], // 10″x12″
21
- [ '11r', 279, 356 ], // 11″x14″
22
- [ '12r', 305, 381 ],
14
+ ['2r', 64, 89],
15
+ ['3r', 89, 127],
16
+ ['4r', 102, 152],
17
+ ['5r', 127, 178], // 5″x7″
18
+ ['6r', 152, 203], // 6″x8″
19
+ ['8r', 203, 254], // 8″x10″
20
+ ['10r', 254, 305], // 10″x12″
21
+ ['11r', 279, 356], // 11″x14″
22
+ ['12r', 305, 381],
23
23
 
24
24
  // Standard Paper Sizes
25
- [ 'a0', 841, 1189 ],
26
- [ 'a1', 594, 841 ],
27
- [ 'a2', 420, 594 ],
28
- [ 'a3', 297, 420 ],
29
- [ 'a4', 210, 297 ],
30
- [ 'a5', 148, 210 ],
31
- [ 'a6', 105, 148 ],
32
- [ 'a7', 74, 105 ],
33
- [ 'a8', 52, 74 ],
34
- [ 'a9', 37, 52 ],
35
- [ 'a10', 26, 37 ],
36
- [ '2a0', 1189, 1682 ],
37
- [ '4a0', 1682, 2378 ],
38
- [ 'b0', 1000, 1414 ],
39
- [ 'b1', 707, 1000 ],
40
- [ 'b1+', 720, 1020 ],
41
- [ 'b2', 500, 707 ],
42
- [ 'b2+', 520, 720 ],
43
- [ 'b3', 353, 500 ],
44
- [ 'b4', 250, 353 ],
45
- [ 'b5', 176, 250 ],
46
- [ 'b6', 125, 176 ],
47
- [ 'b7', 88, 125 ],
48
- [ 'b8', 62, 88 ],
49
- [ 'b9', 44, 62 ],
50
- [ 'b10', 31, 44 ],
51
- [ 'b11', 22, 32 ],
52
- [ 'b12', 16, 22 ],
53
- [ 'c0', 917, 1297 ],
54
- [ 'c1', 648, 917 ],
55
- [ 'c2', 458, 648 ],
56
- [ 'c3', 324, 458 ],
57
- [ 'c4', 229, 324 ],
58
- [ 'c5', 162, 229 ],
59
- [ 'c6', 114, 162 ],
60
- [ 'c7', 81, 114 ],
61
- [ 'c8', 57, 81 ],
62
- [ 'c9', 40, 57 ],
63
- [ 'c10', 28, 40 ],
64
- [ 'c11', 22, 32 ],
65
- [ 'c12', 16, 22 ],
25
+ ['a0', 841, 1189],
26
+ ['a1', 594, 841],
27
+ ['a2', 420, 594],
28
+ ['a3', 297, 420],
29
+ ['a4', 210, 297],
30
+ ['a5', 148, 210],
31
+ ['a6', 105, 148],
32
+ ['a7', 74, 105],
33
+ ['a8', 52, 74],
34
+ ['a9', 37, 52],
35
+ ['a10', 26, 37],
36
+ ['2a0', 1189, 1682],
37
+ ['4a0', 1682, 2378],
38
+ ['b0', 1000, 1414],
39
+ ['b1', 707, 1000],
40
+ ['b1+', 720, 1020],
41
+ ['b2', 500, 707],
42
+ ['b2+', 520, 720],
43
+ ['b3', 353, 500],
44
+ ['b4', 250, 353],
45
+ ['b5', 176, 250],
46
+ ['b6', 125, 176],
47
+ ['b7', 88, 125],
48
+ ['b8', 62, 88],
49
+ ['b9', 44, 62],
50
+ ['b10', 31, 44],
51
+ ['b11', 22, 32],
52
+ ['b12', 16, 22],
53
+ ['c0', 917, 1297],
54
+ ['c1', 648, 917],
55
+ ['c2', 458, 648],
56
+ ['c3', 324, 458],
57
+ ['c4', 229, 324],
58
+ ['c5', 162, 229],
59
+ ['c6', 114, 162],
60
+ ['c7', 81, 114],
61
+ ['c8', 57, 81],
62
+ ['c9', 40, 57],
63
+ ['c10', 28, 40],
64
+ ['c11', 22, 32],
65
+ ['c12', 16, 22],
66
66
 
67
67
  // Use inches for North American sizes,
68
68
  // as it produces less float precision errors
69
- [ 'half-letter', 5.5, 8.5, 'in' ],
70
- [ 'letter', 8.5, 11, 'in' ],
71
- [ 'legal', 8.5, 14, 'in' ],
72
- [ 'junior-legal', 5, 8, 'in' ],
73
- [ 'ledger', 11, 17, 'in' ],
74
- [ 'tabloid', 11, 17, 'in' ],
75
- [ 'ansi-a', 8.5, 11.0, 'in' ],
76
- [ 'ansi-b', 11.0, 17.0, 'in' ],
77
- [ 'ansi-c', 17.0, 22.0, 'in' ],
78
- [ 'ansi-d', 22.0, 34.0, 'in' ],
79
- [ 'ansi-e', 34.0, 44.0, 'in' ],
80
- [ 'arch-a', 9, 12, 'in' ],
81
- [ 'arch-b', 12, 18, 'in' ],
82
- [ 'arch-c', 18, 24, 'in' ],
83
- [ 'arch-d', 24, 36, 'in' ],
84
- [ 'arch-e', 36, 48, 'in' ],
85
- [ 'arch-e1', 30, 42, 'in' ],
86
- [ 'arch-e2', 26, 38, 'in' ],
87
- [ 'arch-e3', 27, 39, 'in' ]
69
+ ['half-letter', 5.5, 8.5, 'in'],
70
+ ['letter', 8.5, 11, 'in'],
71
+ ['legal', 8.5, 14, 'in'],
72
+ ['junior-legal', 5, 8, 'in'],
73
+ ['ledger', 11, 17, 'in'],
74
+ ['tabloid', 11, 17, 'in'],
75
+ ['ansi-a', 8.5, 11.0, 'in'],
76
+ ['ansi-b', 11.0, 17.0, 'in'],
77
+ ['ansi-c', 17.0, 22.0, 'in'],
78
+ ['ansi-d', 22.0, 34.0, 'in'],
79
+ ['ansi-e', 34.0, 44.0, 'in'],
80
+ ['arch-a', 9, 12, 'in'],
81
+ ['arch-b', 12, 18, 'in'],
82
+ ['arch-c', 18, 24, 'in'],
83
+ ['arch-d', 24, 36, 'in'],
84
+ ['arch-e', 36, 48, 'in'],
85
+ ['arch-e1', 30, 42, 'in'],
86
+ ['arch-e2', 26, 38, 'in'],
87
+ ['arch-e3', 27, 39, 'in'],
88
88
  ];
89
89
 
90
90
  let dictionary = data.reduce((dict, preset) => {
91
91
  const item = {
92
92
  units: preset[3] || defaultUnits,
93
- dimensions: [ preset[1], preset[2] ]
93
+ dimensions: [preset[1], preset[2]],
94
94
  };
95
95
 
96
96
  dict[preset[0]] = item;
97
- dict[preset[0].replace(/-/g, ' ')] = item;
98
97
 
99
98
  return dict;
100
99
  }, {});
@@ -1,12 +1,32 @@
1
- import paperSizes from "./paper-sizes.js";
2
- import convertLength from "convert-length";
1
+ import paperSizes from './paper-sizes.js';
2
+ import convertLength from 'convert-length';
3
+
4
+ export const PRESET_ORIENTATIONS = {
5
+ LANDSCAPE: 'landscape',
6
+ PORTRAIT: 'portrait',
7
+ };
3
8
 
4
9
  export default Object.keys(paperSizes);
5
10
 
6
- export function getDimensionsForPreset(preset, { pixelsPerInch }) {
11
+ /**
12
+ * Compute the dimensions for a given preset
13
+ * @param {string} preset
14
+ * @param {object} params
15
+ * @param {number} params.pixelsPerInch
16
+ * @param {string} params.orientation
17
+ * @returns {number[]} dimensions
18
+ */
19
+ export function getDimensionsForPreset(
20
+ preset,
21
+ { pixelsPerInch, orientation = PRESET_ORIENTATIONS.PORTRAIT },
22
+ ) {
7
23
  const { dimensions, units } = paperSizes[preset];
8
24
 
9
- return dimensions.map(n => {
10
- return convertLength(n, units, "px", { pixelsPerInch });
25
+ const [width, height] = dimensions.map((n) => {
26
+ return convertLength(n, units, 'px', { pixelsPerInch });
11
27
  });
28
+
29
+ return orientation === PRESET_ORIENTATIONS.PORTRAIT
30
+ ? [width, height]
31
+ : [height, width];
12
32
  }