@revideo/2d 0.2.5 → 0.2.6-alpha.906

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 (222) hide show
  1. package/lib/code/CodeCursor.d.ts +74 -74
  2. package/lib/code/CodeCursor.js +286 -352
  3. package/lib/code/CodeDiffer.d.ts +16 -21
  4. package/lib/code/CodeDiffer.js +41 -39
  5. package/lib/code/CodeFragment.d.ts +8 -16
  6. package/lib/code/CodeFragment.js +36 -36
  7. package/lib/code/CodeHighlighter.d.ts +59 -59
  8. package/lib/code/CodeHighlighter.js +1 -1
  9. package/lib/code/CodeMetrics.d.ts +8 -12
  10. package/lib/code/CodeMetrics.js +24 -26
  11. package/lib/code/CodeRange.d.ts +5 -21
  12. package/lib/code/CodeRange.js +124 -131
  13. package/lib/code/CodeScope.d.ts +8 -18
  14. package/lib/code/CodeScope.js +61 -59
  15. package/lib/code/CodeSelection.d.ts +4 -9
  16. package/lib/code/CodeSelection.js +8 -8
  17. package/lib/code/CodeSignal.d.ts +47 -74
  18. package/lib/code/CodeSignal.js +189 -206
  19. package/lib/code/CodeTokenizer.d.ts +1 -1
  20. package/lib/code/CodeTokenizer.js +41 -41
  21. package/lib/code/DefaultHighlightStyle.d.ts +2 -2
  22. package/lib/code/DefaultHighlightStyle.js +96 -96
  23. package/lib/code/LezerHighlighter.d.ts +19 -21
  24. package/lib/code/LezerHighlighter.js +101 -101
  25. package/lib/code/diff.d.ts +9 -12
  26. package/lib/code/diff.js +205 -218
  27. package/lib/code/extractRange.d.ts +4 -7
  28. package/lib/code/extractRange.js +71 -79
  29. package/lib/code/index.d.ts +1 -1
  30. package/lib/code/index.js +1 -1
  31. package/lib/components/Audio.d.ts +11 -11
  32. package/lib/components/Audio.d.ts.map +1 -1
  33. package/lib/components/Audio.js +109 -115
  34. package/lib/components/Bezier.d.ts +18 -18
  35. package/lib/components/Bezier.js +75 -86
  36. package/lib/components/Circle.d.ts +90 -95
  37. package/lib/components/Circle.js +74 -85
  38. package/lib/components/Code.d.ts +211 -244
  39. package/lib/components/Code.d.ts.map +1 -1
  40. package/lib/components/Code.js +269 -317
  41. package/lib/components/CodeBlock.d.ts +67 -109
  42. package/lib/components/CodeBlock.d.ts.map +1 -1
  43. package/lib/components/CodeBlock.js +373 -414
  44. package/lib/components/CubicBezier.d.ts +36 -36
  45. package/lib/components/CubicBezier.js +49 -52
  46. package/lib/components/Curve.d.ts +198 -208
  47. package/lib/components/Curve.js +269 -270
  48. package/lib/components/Grid.d.ts +42 -47
  49. package/lib/components/Grid.js +56 -63
  50. package/lib/components/Icon.d.ts +50 -55
  51. package/lib/components/Icon.js +50 -58
  52. package/lib/components/Img.d.ts +79 -86
  53. package/lib/components/Img.d.ts.map +1 -1
  54. package/lib/components/Img.js +181 -193
  55. package/lib/components/Knot.d.ts +78 -97
  56. package/lib/components/Knot.js +60 -81
  57. package/lib/components/Latex.d.ts +12 -12
  58. package/lib/components/Latex.js +65 -77
  59. package/lib/components/Layout.d.ts +413 -455
  60. package/lib/components/Layout.d.ts.map +1 -1
  61. package/lib/components/Layout.js +681 -666
  62. package/lib/components/Line.d.ts +49 -61
  63. package/lib/components/Line.js +193 -216
  64. package/lib/components/Media.d.ts +35 -35
  65. package/lib/components/Media.d.ts.map +1 -1
  66. package/lib/components/Media.js +134 -135
  67. package/lib/components/Node.d.ts +830 -875
  68. package/lib/components/Node.d.ts.map +1 -1
  69. package/lib/components/Node.js +1314 -1383
  70. package/lib/components/Path.d.ts +14 -35
  71. package/lib/components/Path.js +89 -100
  72. package/lib/components/Polygon.d.ts +55 -60
  73. package/lib/components/Polygon.js +66 -71
  74. package/lib/components/QuadBezier.d.ts +29 -29
  75. package/lib/components/QuadBezier.js +45 -50
  76. package/lib/components/Ray.d.ts +28 -28
  77. package/lib/components/Ray.js +64 -71
  78. package/lib/components/Rect.d.ts +108 -115
  79. package/lib/components/Rect.js +72 -78
  80. package/lib/components/SVG.d.ts +139 -165
  81. package/lib/components/SVG.js +541 -609
  82. package/lib/components/Shape.d.ts +35 -44
  83. package/lib/components/Shape.d.ts.map +1 -1
  84. package/lib/components/Shape.js +127 -112
  85. package/lib/components/Spline.d.ts +40 -46
  86. package/lib/components/Spline.js +175 -190
  87. package/lib/components/Txt.d.ts +44 -55
  88. package/lib/components/Txt.d.ts.map +1 -1
  89. package/lib/components/Txt.js +155 -156
  90. package/lib/components/TxtLeaf.d.ts +16 -20
  91. package/lib/components/TxtLeaf.d.ts.map +1 -1
  92. package/lib/components/TxtLeaf.js +2 -2
  93. package/lib/components/Video.d.ts +45 -41
  94. package/lib/components/Video.d.ts.map +1 -1
  95. package/lib/components/Video.js +207 -138
  96. package/lib/components/View2D.d.ts +22 -21
  97. package/lib/components/View2D.d.ts.map +1 -1
  98. package/lib/components/View2D.js +89 -98
  99. package/lib/components/index.d.ts +1 -1
  100. package/lib/components/index.js +1 -1
  101. package/lib/components/types.d.ts +9 -22
  102. package/lib/components/types.js +1 -1
  103. package/lib/curves/ArcSegment.d.ts +24 -36
  104. package/lib/curves/ArcSegment.js +94 -145
  105. package/lib/curves/CircleSegment.d.ts +16 -26
  106. package/lib/curves/CircleSegment.js +49 -56
  107. package/lib/curves/CubicBezierSegment.d.ts +13 -18
  108. package/lib/curves/CubicBezierSegment.js +46 -79
  109. package/lib/curves/CurveDrawingInfo.d.ts +9 -9
  110. package/lib/curves/CurveDrawingInfo.js +1 -1
  111. package/lib/curves/CurvePoint.d.ts +13 -13
  112. package/lib/curves/CurvePoint.js +1 -1
  113. package/lib/curves/CurveProfile.d.ts +5 -5
  114. package/lib/curves/CurveProfile.js +1 -1
  115. package/lib/curves/KnotInfo.d.ts +8 -8
  116. package/lib/curves/KnotInfo.js +1 -1
  117. package/lib/curves/LineSegment.d.ts +14 -19
  118. package/lib/curves/LineSegment.js +42 -42
  119. package/lib/curves/Polynomial.d.ts +110 -110
  120. package/lib/curves/Polynomial.js +239 -249
  121. package/lib/curves/Polynomial2D.d.ts +20 -20
  122. package/lib/curves/Polynomial2D.js +42 -46
  123. package/lib/curves/PolynomialSegment.d.ts +37 -42
  124. package/lib/curves/PolynomialSegment.js +81 -83
  125. package/lib/curves/QuadBezierSegment.d.ts +12 -12
  126. package/lib/curves/QuadBezierSegment.js +41 -71
  127. package/lib/curves/Segment.d.ts +7 -12
  128. package/lib/curves/Segment.js +3 -2
  129. package/lib/curves/UniformPolynomialCurveSampler.d.ts +34 -34
  130. package/lib/curves/UniformPolynomialCurveSampler.js +62 -68
  131. package/lib/curves/createCurveProfileLerp.d.ts +6 -17
  132. package/lib/curves/createCurveProfileLerp.js +208 -211
  133. package/lib/curves/getBezierSplineProfile.d.ts +4 -8
  134. package/lib/curves/getBezierSplineProfile.js +101 -145
  135. package/lib/curves/getCircleProfile.d.ts +4 -10
  136. package/lib/curves/getCircleProfile.js +40 -72
  137. package/lib/curves/getPathProfile.d.ts +2 -2
  138. package/lib/curves/getPathProfile.js +112 -135
  139. package/lib/curves/getPointAtDistance.d.ts +4 -7
  140. package/lib/curves/getPointAtDistance.js +12 -12
  141. package/lib/curves/getPolylineProfile.d.ts +4 -8
  142. package/lib/curves/getPolylineProfile.js +54 -67
  143. package/lib/curves/getRectProfile.d.ts +4 -9
  144. package/lib/curves/getRectProfile.js +52 -117
  145. package/lib/curves/index.d.ts +1 -1
  146. package/lib/curves/index.js +1 -1
  147. package/lib/decorators/canvasStyleSignal.d.ts +3 -3
  148. package/lib/decorators/canvasStyleSignal.js +10 -10
  149. package/lib/decorators/colorSignal.d.ts +1 -1
  150. package/lib/decorators/colorSignal.js +7 -7
  151. package/lib/decorators/compound.d.ts +2 -4
  152. package/lib/decorators/compound.js +23 -43
  153. package/lib/decorators/computed.d.ts +1 -1
  154. package/lib/decorators/computed.js +9 -9
  155. package/lib/decorators/defaultStyle.d.ts +2 -5
  156. package/lib/decorators/defaultStyle.js +11 -11
  157. package/lib/decorators/filtersSignal.d.ts +8 -28
  158. package/lib/decorators/filtersSignal.js +64 -87
  159. package/lib/decorators/index.d.ts +1 -1
  160. package/lib/decorators/index.js +1 -1
  161. package/lib/decorators/initializers.d.ts +2 -5
  162. package/lib/decorators/initializers.js +19 -18
  163. package/lib/decorators/nodeName.d.ts +1 -1
  164. package/lib/decorators/nodeName.js +4 -4
  165. package/lib/decorators/signal.d.ts +20 -40
  166. package/lib/decorators/signal.js +104 -113
  167. package/lib/decorators/spacingSignal.d.ts +1 -1
  168. package/lib/decorators/spacingSignal.js +13 -13
  169. package/lib/decorators/vector2Signal.d.ts +7 -13
  170. package/lib/decorators/vector2Signal.js +13 -15
  171. package/lib/index.d.ts +1 -1
  172. package/lib/index.js +1 -1
  173. package/lib/jsx-dev-runtime.d.ts +3 -3
  174. package/lib/jsx-dev-runtime.js +3 -3
  175. package/lib/jsx-runtime.d.ts +9 -19
  176. package/lib/jsx-runtime.js +18 -18
  177. package/lib/partials/Filter.d.ts +16 -24
  178. package/lib/partials/Filter.js +69 -69
  179. package/lib/partials/Gradient.d.ts +24 -30
  180. package/lib/partials/Gradient.js +58 -69
  181. package/lib/partials/Pattern.d.ts +9 -14
  182. package/lib/partials/Pattern.js +24 -30
  183. package/lib/partials/ShaderConfig.d.ts +74 -81
  184. package/lib/partials/ShaderConfig.js +23 -22
  185. package/lib/partials/index.d.ts +1 -1
  186. package/lib/partials/index.js +1 -1
  187. package/lib/partials/types.d.ts +6 -19
  188. package/lib/partials/types.js +1 -1
  189. package/lib/scenes/Scene2D.d.ts +23 -42
  190. package/lib/scenes/Scene2D.d.ts.map +1 -1
  191. package/lib/scenes/Scene2D.js +152 -161
  192. package/lib/scenes/index.d.ts +1 -1
  193. package/lib/scenes/index.js +1 -1
  194. package/lib/scenes/makeScene2D.d.ts +5 -7
  195. package/lib/scenes/makeScene2D.js +10 -10
  196. package/lib/scenes/useScene2D.d.ts +2 -2
  197. package/lib/scenes/useScene2D.js +3 -3
  198. package/lib/tsconfig.build.tsbuildinfo +1 -1
  199. package/lib/utils/CanvasUtils.d.ts +21 -93
  200. package/lib/utils/CanvasUtils.js +105 -186
  201. package/lib/utils/diff.d.ts +18 -25
  202. package/lib/utils/diff.js +87 -84
  203. package/lib/utils/index.d.ts +1 -1
  204. package/lib/utils/index.js +1 -1
  205. package/lib/utils/is.d.ts +2 -4
  206. package/lib/utils/is.js +2 -2
  207. package/lib/utils/makeSignalExtensions.d.ts +4 -11
  208. package/lib/utils/makeSignalExtensions.js +17 -17
  209. package/package.json +5 -5
  210. package/src/lib/components/Audio.ts +2 -2
  211. package/src/lib/components/Code.ts +4 -2
  212. package/src/lib/components/CodeBlock.ts +1 -1
  213. package/src/lib/components/Img.ts +2 -2
  214. package/src/lib/components/Layout.ts +2 -2
  215. package/src/lib/components/Media.ts +3 -1
  216. package/src/lib/components/Node.ts +9 -9
  217. package/src/lib/components/Shape.ts +2 -2
  218. package/src/lib/components/Txt.ts +2 -2
  219. package/src/lib/components/TxtLeaf.ts +1 -1
  220. package/src/lib/components/Video.ts +120 -8
  221. package/src/lib/components/View2D.ts +6 -2
  222. package/src/lib/scenes/Scene2D.ts +4 -3
@@ -23,6 +23,71 @@ export interface VideoProps extends MediaProps {
23
23
  smoothing?: SignalValue<boolean>;
24
24
  }
25
25
 
26
+ class ImageCommunication {
27
+ public constructor() {
28
+ if (!import.meta.hot) {
29
+ throw new Error('FfmpegVideoFrame can only be used with HMR.');
30
+ }
31
+
32
+ import.meta.hot.on(
33
+ 'revideo/ffmpeg-video-frame-res',
34
+ this.handler.bind(this),
35
+ );
36
+ }
37
+
38
+ private nextFrameHandlers: ((event: MessageEvent) => void)[] = [];
39
+
40
+ private handler(event: MessageEvent) {
41
+ const handlers = this.nextFrameHandlers;
42
+ this.nextFrameHandlers = [];
43
+
44
+ for (const handler of handlers) {
45
+ handler(event);
46
+ }
47
+ }
48
+
49
+ public async getFrame(
50
+ id: string,
51
+ src: string,
52
+ time: number,
53
+ duration: number,
54
+ fps: number,
55
+ ) {
56
+ return new Promise<HTMLImageElement>((resolve, reject) => {
57
+ if (!import.meta.hot) {
58
+ reject('FfmpegVideoFrame can only be used with HMR.');
59
+ return;
60
+ }
61
+
62
+ function handler(event: MessageEvent) {
63
+ const image = new Image();
64
+
65
+ const uint8Array = new Uint8Array(event.data.frame.data);
66
+ const blob = new Blob([uint8Array], {type: 'image/jpeg'});
67
+ const url = URL.createObjectURL(blob);
68
+
69
+ image.src = url;
70
+
71
+ image.onload = () => {
72
+ resolve(image);
73
+ };
74
+ }
75
+
76
+ this.nextFrameHandlers.push(handler);
77
+
78
+ import.meta.hot.send('revideo/ffmpeg-video-frame', {
79
+ data: {
80
+ id: id,
81
+ filePath: src,
82
+ startTime: time,
83
+ duration,
84
+ fps,
85
+ },
86
+ });
87
+ });
88
+ }
89
+ }
90
+
26
91
  @nodeName('Video')
27
92
  export class Video extends Media {
28
93
  /**
@@ -51,6 +116,10 @@ export class Video extends Media {
51
116
 
52
117
  private static readonly pool: Record<string, HTMLVideoElement> = {};
53
118
 
119
+ private static readonly imageCommunication = !import.meta.hot
120
+ ? null
121
+ : new ImageCommunication();
122
+
54
123
  public constructor(props: VideoProps) {
55
124
  super(props);
56
125
  }
@@ -159,16 +228,59 @@ export class Video extends Media {
159
228
  return video;
160
229
  }
161
230
 
162
- protected override draw(context: CanvasRenderingContext2D) {
231
+ protected lastFrame: HTMLImageElement | null = null;
232
+
233
+ protected async serverSeekedVideo(): Promise<HTMLImageElement> {
234
+ const video = this.video();
235
+ const time = this.clampTime(this.time());
236
+ const duration = this.getDuration();
237
+
238
+ video.playbackRate = this.playbackRate();
239
+
240
+ if (this.lastFrame && this.lastTime === time) {
241
+ return this.lastFrame;
242
+ }
243
+
244
+ const fps = this.view().fps() / this.playbackRate();
245
+
246
+ if (!Video.imageCommunication) {
247
+ throw new Error('ServerSeekedVideo can only be used with HMR.');
248
+ }
249
+
250
+ const frame = await Video.imageCommunication.getFrame(
251
+ this.key,
252
+ video.src,
253
+ time,
254
+ duration,
255
+ fps,
256
+ );
257
+ this.lastFrame = frame;
258
+ this.lastTime = time;
259
+
260
+ return frame;
261
+ }
262
+
263
+ protected async seekFunction() {
264
+ const playbackState = this.view().playbackState();
265
+ if (
266
+ playbackState === PlaybackState.Playing ||
267
+ playbackState === PlaybackState.Presenting
268
+ ) {
269
+ return this.fastSeekedVideo();
270
+ }
271
+
272
+ if (playbackState === PlaybackState.Rendering) {
273
+ return this.serverSeekedVideo();
274
+ }
275
+
276
+ return this.seekedVideo();
277
+ }
278
+
279
+ protected override async draw(context: CanvasRenderingContext2D) {
163
280
  this.drawShape(context);
164
281
  const alpha = this.alpha();
165
282
  if (alpha > 0) {
166
- const playbackState = this.view().playbackState();
167
- const video =
168
- playbackState === PlaybackState.Playing ||
169
- playbackState === PlaybackState.Presenting
170
- ? this.fastSeekedVideo()
171
- : this.seekedVideo();
283
+ const video = await this.seekFunction();
172
284
 
173
285
  const box = BBox.fromSizeCentered(this.computedSize());
174
286
  context.save();
@@ -185,7 +297,7 @@ export class Video extends Media {
185
297
  context.clip(this.getPath());
186
298
  }
187
299
 
188
- this.drawChildren(context);
300
+ await this.drawChildren(context);
189
301
  }
190
302
 
191
303
  protected override applyFlex() {
@@ -37,6 +37,10 @@ export class View2D extends Rect {
37
37
  @signal()
38
38
  public declare readonly globalTime: SimpleSignal<number, this>;
39
39
 
40
+ @initial(0)
41
+ @signal()
42
+ public declare readonly fps: SimpleSignal<number, this>;
43
+
40
44
  @signal()
41
45
  public declare readonly assetHash: SimpleSignal<string, this>;
42
46
 
@@ -61,10 +65,10 @@ export class View2D extends Rect {
61
65
  super.dispose();
62
66
  }
63
67
 
64
- public override render(context: CanvasRenderingContext2D) {
68
+ public override async render(context: CanvasRenderingContext2D) {
65
69
  this.computedSize();
66
70
  this.computedPosition();
67
- super.render(context);
71
+ await super.render(context);
68
72
  }
69
73
 
70
74
  /**
@@ -43,15 +43,16 @@ export class Scene2D extends GeneratorScene<View2D> implements Inspectable {
43
43
  return super.next();
44
44
  }
45
45
 
46
- public draw(context: CanvasRenderingContext2D) {
46
+ public async draw(context: CanvasRenderingContext2D) {
47
47
  context.save();
48
48
  this.renderLifecycle.dispatch([SceneRenderEvent.BeforeRender, context]);
49
49
  context.save();
50
50
  this.renderLifecycle.dispatch([SceneRenderEvent.BeginRender, context]);
51
51
  this.getView()
52
52
  .playbackState(this.playback.state)
53
- .globalTime(this.playback.time);
54
- this.getView().render(context);
53
+ .globalTime(this.playback.time)
54
+ .fps(this.playback.fps);
55
+ await this.getView().render(context);
55
56
  this.renderLifecycle.dispatch([SceneRenderEvent.FinishRender, context]);
56
57
  context.restore();
57
58
  this.renderLifecycle.dispatch([SceneRenderEvent.AfterRender, context]);