samcan 0.0.2 → 0.1.0
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.
- package/{license → LICENSE} +3 -1
- package/README.md +3 -0
- package/dist/camera/index.cjs +370 -0
- package/dist/camera/index.d.cts +54 -0
- package/dist/camera/index.d.ts +54 -0
- package/dist/camera/index.js +70 -0
- package/dist/document/index.cjs +735 -0
- package/dist/document/index.d.cts +174 -0
- package/dist/document/index.d.ts +174 -0
- package/dist/document/index.js +161 -0
- package/dist/editor/index.cjs +5007 -0
- package/dist/editor/index.d.cts +374 -0
- package/dist/editor/index.d.ts +374 -0
- package/dist/editor/index.js +2401 -0
- package/dist/engine/index.cjs +3752 -0
- package/dist/engine/index.d.cts +183 -0
- package/dist/engine/index.d.ts +183 -0
- package/dist/engine/index.js +111 -0
- package/dist/index.cjs +8899 -0
- package/dist/index.d.cts +962 -0
- package/dist/index.d.ts +962 -46
- package/dist/index.js +1008 -49
- package/dist/math/index.cjs +2230 -0
- package/dist/math/index.d.cts +302 -0
- package/dist/math/index.d.ts +302 -0
- package/dist/math/index.js +1109 -0
- package/dist/renderer/index.cjs +1865 -0
- package/dist/renderer/index.d.cts +91 -0
- package/dist/renderer/index.d.ts +91 -0
- package/dist/renderer/index.js +89 -0
- package/dist/shared/chunk-35pkr0zs.js +110 -0
- package/dist/shared/chunk-7sr7q84y.js +80 -0
- package/dist/shared/chunk-87399bz7.js +89 -0
- package/dist/shared/chunk-8ynnz57m.js +129 -0
- package/dist/shared/chunk-d6gxvapt.js +500 -0
- package/dist/shared/chunk-hewznwbd.js +305 -0
- package/dist/shared/chunk-jdvrd5tv.js +623 -0
- package/dist/shared/chunk-rvag1j46.js +461 -0
- package/dist/shared/chunk-rzxyjwk8.js +397 -0
- package/dist/shared/chunk-s3qdcmcq.js +1392 -0
- package/dist/shared/chunk-v53jprrn.js +68 -0
- package/dist/shared/chunk-wa3772hp.js +117 -0
- package/dist/spatial/index.cjs +954 -0
- package/dist/spatial/index.d.cts +136 -0
- package/dist/spatial/index.d.ts +136 -0
- package/dist/spatial/index.js +301 -0
- package/dist/stroke/index.cjs +1010 -0
- package/dist/stroke/index.d.cts +143 -0
- package/dist/stroke/index.d.ts +143 -0
- package/dist/stroke/index.js +343 -0
- package/package.json +122 -58
- package/dist/bundle.js +0 -5888
- package/dist/core/animation/animationruntime.d.ts +0 -238
- package/dist/core/animation/animationruntime.d.ts.map +0 -1
- package/dist/core/animation/animationruntime.js +0 -530
- package/dist/core/animation/animationruntime.js.map +0 -1
- package/dist/core/animation/animationstate.d.ts +0 -85
- package/dist/core/animation/animationstate.d.ts.map +0 -1
- package/dist/core/animation/animationstate.js +0 -119
- package/dist/core/animation/animationstate.js.map +0 -1
- package/dist/core/animation/animationtrack.d.ts +0 -55
- package/dist/core/animation/animationtrack.d.ts.map +0 -1
- package/dist/core/animation/animationtrack.js +0 -185
- package/dist/core/animation/animationtrack.js.map +0 -1
- package/dist/core/animation/easing.d.ts +0 -80
- package/dist/core/animation/easing.d.ts.map +0 -1
- package/dist/core/animation/easing.js +0 -126
- package/dist/core/animation/easing.js.map +0 -1
- package/dist/core/animation/index.d.ts +0 -11
- package/dist/core/animation/index.d.ts.map +0 -1
- package/dist/core/animation/index.js +0 -10
- package/dist/core/animation/index.js.map +0 -1
- package/dist/core/animation/interpolator.d.ts +0 -82
- package/dist/core/animation/interpolator.d.ts.map +0 -1
- package/dist/core/animation/interpolator.js +0 -108
- package/dist/core/animation/interpolator.js.map +0 -1
- package/dist/core/animation/keyframe.d.ts +0 -52
- package/dist/core/animation/keyframe.d.ts.map +0 -1
- package/dist/core/animation/keyframe.js +0 -65
- package/dist/core/animation/keyframe.js.map +0 -1
- package/dist/core/animation/logger.d.ts +0 -8
- package/dist/core/animation/logger.d.ts.map +0 -1
- package/dist/core/animation/logger.js +0 -20
- package/dist/core/animation/logger.js.map +0 -1
- package/dist/core/animation/statemachine.d.ts +0 -178
- package/dist/core/animation/statemachine.d.ts.map +0 -1
- package/dist/core/animation/statemachine.js +0 -378
- package/dist/core/animation/statemachine.js.map +0 -1
- package/dist/core/animation/statetransition.d.ts +0 -142
- package/dist/core/animation/statetransition.d.ts.map +0 -1
- package/dist/core/animation/statetransition.js +0 -189
- package/dist/core/animation/statetransition.js.map +0 -1
- package/dist/core/animation/timeline.d.ts +0 -62
- package/dist/core/animation/timeline.d.ts.map +0 -1
- package/dist/core/animation/timeline.js +0 -102
- package/dist/core/animation/timeline.js.map +0 -1
- package/dist/core/api.d.ts +0 -245
- package/dist/core/api.d.ts.map +0 -1
- package/dist/core/api.js +0 -369
- package/dist/core/api.js.map +0 -1
- package/dist/core/asset/assetmanager.d.ts +0 -196
- package/dist/core/asset/assetmanager.d.ts.map +0 -1
- package/dist/core/asset/assetmanager.js +0 -502
- package/dist/core/asset/assetmanager.js.map +0 -1
- package/dist/core/asset/index.d.ts +0 -3
- package/dist/core/asset/index.d.ts.map +0 -1
- package/dist/core/asset/index.js +0 -3
- package/dist/core/asset/index.js.map +0 -1
- package/dist/core/asset/types.d.ts +0 -36
- package/dist/core/asset/types.d.ts.map +0 -1
- package/dist/core/asset/types.js +0 -1
- package/dist/core/asset/types.js.map +0 -1
- package/dist/core/command/basecommand.d.ts +0 -29
- package/dist/core/command/basecommand.d.ts.map +0 -1
- package/dist/core/command/basecommand.js +0 -36
- package/dist/core/command/basecommand.js.map +0 -1
- package/dist/core/command/command.d.ts +0 -55
- package/dist/core/command/command.d.ts.map +0 -1
- package/dist/core/command/command.js +0 -1
- package/dist/core/command/command.js.map +0 -1
- package/dist/core/command/commandhistory.d.ts +0 -76
- package/dist/core/command/commandhistory.d.ts.map +0 -1
- package/dist/core/command/commandhistory.js +0 -122
- package/dist/core/command/commandhistory.js.map +0 -1
- package/dist/core/command/editorcommands.d.ts +0 -108
- package/dist/core/command/editorcommands.d.ts.map +0 -1
- package/dist/core/command/editorcommands.js +0 -274
- package/dist/core/command/editorcommands.js.map +0 -1
- package/dist/core/command/index.d.ts +0 -5
- package/dist/core/command/index.d.ts.map +0 -1
- package/dist/core/command/index.js +0 -5
- package/dist/core/command/index.js.map +0 -1
- package/dist/core/editor/events/emitter.d.ts +0 -45
- package/dist/core/editor/events/emitter.d.ts.map +0 -1
- package/dist/core/editor/events/emitter.js +0 -88
- package/dist/core/editor/events/emitter.js.map +0 -1
- package/dist/core/error/animationerror.d.ts +0 -27
- package/dist/core/error/animationerror.d.ts.map +0 -1
- package/dist/core/error/animationerror.js +0 -41
- package/dist/core/error/animationerror.js.map +0 -1
- package/dist/core/error/asseterror.d.ts +0 -32
- package/dist/core/error/asseterror.d.ts.map +0 -1
- package/dist/core/error/asseterror.js +0 -48
- package/dist/core/error/asseterror.js.map +0 -1
- package/dist/core/error/index.d.ts +0 -7
- package/dist/core/error/index.d.ts.map +0 -1
- package/dist/core/error/index.js +0 -7
- package/dist/core/error/index.js.map +0 -1
- package/dist/core/error/pluginerror.d.ts +0 -22
- package/dist/core/error/pluginerror.d.ts.map +0 -1
- package/dist/core/error/pluginerror.js +0 -33
- package/dist/core/error/pluginerror.js.map +0 -1
- package/dist/core/error/renderererror.d.ts +0 -26
- package/dist/core/error/renderererror.d.ts.map +0 -1
- package/dist/core/error/renderererror.js +0 -40
- package/dist/core/error/renderererror.js.map +0 -1
- package/dist/core/error/samcanerror.d.ts +0 -72
- package/dist/core/error/samcanerror.d.ts.map +0 -1
- package/dist/core/error/samcanerror.js +0 -108
- package/dist/core/error/samcanerror.js.map +0 -1
- package/dist/core/error/serializationerror.d.ts +0 -31
- package/dist/core/error/serializationerror.d.ts.map +0 -1
- package/dist/core/error/serializationerror.js +0 -46
- package/dist/core/error/serializationerror.js.map +0 -1
- package/dist/core/index.d.ts +0 -12
- package/dist/core/index.d.ts.map +0 -1
- package/dist/core/index.js +0 -12
- package/dist/core/index.js.map +0 -1
- package/dist/core/math/color.d.ts +0 -56
- package/dist/core/math/color.d.ts.map +0 -1
- package/dist/core/math/color.js +0 -125
- package/dist/core/math/color.js.map +0 -1
- package/dist/core/math/index.d.ts +0 -14
- package/dist/core/math/index.d.ts.map +0 -1
- package/dist/core/math/index.js +0 -14
- package/dist/core/math/index.js.map +0 -1
- package/dist/core/math/matrix.d.ts +0 -101
- package/dist/core/math/matrix.d.ts.map +0 -1
- package/dist/core/math/matrix.js +0 -177
- package/dist/core/math/matrix.js.map +0 -1
- package/dist/core/math/paint.d.ts +0 -104
- package/dist/core/math/paint.d.ts.map +0 -1
- package/dist/core/math/paint.js +0 -204
- package/dist/core/math/paint.js.map +0 -1
- package/dist/core/math/path/index.d.ts +0 -94
- package/dist/core/math/path/index.d.ts.map +0 -1
- package/dist/core/math/path/index.js +0 -180
- package/dist/core/math/path/index.js.map +0 -1
- package/dist/core/math/path/operations.d.ts +0 -38
- package/dist/core/math/path/operations.d.ts.map +0 -1
- package/dist/core/math/path/operations.js +0 -149
- package/dist/core/math/path/operations.js.map +0 -1
- package/dist/core/math/pool.d.ts +0 -54
- package/dist/core/math/pool.d.ts.map +0 -1
- package/dist/core/math/pool.js +0 -97
- package/dist/core/math/pool.js.map +0 -1
- package/dist/core/math/pools.d.ts +0 -29
- package/dist/core/math/pools.d.ts.map +0 -1
- package/dist/core/math/pools.js +0 -50
- package/dist/core/math/pools.js.map +0 -1
- package/dist/core/math/rectangle.d.ts +0 -68
- package/dist/core/math/rectangle.d.ts.map +0 -1
- package/dist/core/math/rectangle.js +0 -124
- package/dist/core/math/rectangle.js.map +0 -1
- package/dist/core/math/utils.d.ts +0 -68
- package/dist/core/math/utils.d.ts.map +0 -1
- package/dist/core/math/utils.js +0 -110
- package/dist/core/math/utils.js.map +0 -1
- package/dist/core/math/vector2.d.ts +0 -85
- package/dist/core/math/vector2.d.ts.map +0 -1
- package/dist/core/math/vector2.js +0 -134
- package/dist/core/math/vector2.js.map +0 -1
- package/dist/core/plugin/animationcontroller.d.ts +0 -43
- package/dist/core/plugin/animationcontroller.d.ts.map +0 -1
- package/dist/core/plugin/animationcontroller.js +0 -11
- package/dist/core/plugin/animationcontroller.js.map +0 -1
- package/dist/core/plugin/index.d.ts +0 -6
- package/dist/core/plugin/index.d.ts.map +0 -1
- package/dist/core/plugin/index.js +0 -4
- package/dist/core/plugin/index.js.map +0 -1
- package/dist/core/plugin/plugin.d.ts +0 -53
- package/dist/core/plugin/plugin.d.ts.map +0 -1
- package/dist/core/plugin/plugin.js +0 -29
- package/dist/core/plugin/plugin.js.map +0 -1
- package/dist/core/plugin/pluginregistry.d.ts +0 -71
- package/dist/core/plugin/pluginregistry.d.ts.map +0 -1
- package/dist/core/plugin/pluginregistry.js +0 -143
- package/dist/core/plugin/pluginregistry.js.map +0 -1
- package/dist/core/renderer/batchmanager.d.ts +0 -68
- package/dist/core/renderer/batchmanager.d.ts.map +0 -1
- package/dist/core/renderer/batchmanager.js +0 -82
- package/dist/core/renderer/batchmanager.js.map +0 -1
- package/dist/core/renderer/canvas2drenderer.d.ts +0 -139
- package/dist/core/renderer/canvas2drenderer.d.ts.map +0 -1
- package/dist/core/renderer/canvas2drenderer.js +0 -499
- package/dist/core/renderer/canvas2drenderer.js.map +0 -1
- package/dist/core/renderer/dirtyregionmanager.d.ts +0 -54
- package/dist/core/renderer/dirtyregionmanager.d.ts.map +0 -1
- package/dist/core/renderer/dirtyregionmanager.js +0 -129
- package/dist/core/renderer/dirtyregionmanager.js.map +0 -1
- package/dist/core/renderer/index.d.ts +0 -8
- package/dist/core/renderer/index.d.ts.map +0 -1
- package/dist/core/renderer/index.js +0 -6
- package/dist/core/renderer/index.js.map +0 -1
- package/dist/core/renderer/renderer.d.ts +0 -154
- package/dist/core/renderer/renderer.d.ts.map +0 -1
- package/dist/core/renderer/renderer.js +0 -1
- package/dist/core/renderer/renderer.js.map +0 -1
- package/dist/core/renderer/rendererfactory.d.ts +0 -66
- package/dist/core/renderer/rendererfactory.d.ts.map +0 -1
- package/dist/core/renderer/rendererfactory.js +0 -219
- package/dist/core/renderer/rendererfactory.js.map +0 -1
- package/dist/core/renderer/webglrenderer.d.ts +0 -185
- package/dist/core/renderer/webglrenderer.d.ts.map +0 -1
- package/dist/core/renderer/webglrenderer.js +0 -1007
- package/dist/core/renderer/webglrenderer.js.map +0 -1
- package/dist/core/scene/index.d.ts +0 -4
- package/dist/core/scene/index.d.ts.map +0 -1
- package/dist/core/scene/index.js +0 -4
- package/dist/core/scene/index.js.map +0 -1
- package/dist/core/scene/node.d.ts +0 -162
- package/dist/core/scene/node.d.ts.map +0 -1
- package/dist/core/scene/node.js +0 -402
- package/dist/core/scene/node.js.map +0 -1
- package/dist/core/scene/nodes/artboard.d.ts +0 -42
- package/dist/core/scene/nodes/artboard.d.ts.map +0 -1
- package/dist/core/scene/nodes/artboard.js +0 -64
- package/dist/core/scene/nodes/artboard.js.map +0 -1
- package/dist/core/scene/nodes/groupnode.d.ts +0 -10
- package/dist/core/scene/nodes/groupnode.d.ts.map +0 -1
- package/dist/core/scene/nodes/groupnode.js +0 -12
- package/dist/core/scene/nodes/groupnode.js.map +0 -1
- package/dist/core/scene/nodes/imagenode.d.ts +0 -38
- package/dist/core/scene/nodes/imagenode.d.ts.map +0 -1
- package/dist/core/scene/nodes/imagenode.js +0 -77
- package/dist/core/scene/nodes/imagenode.js.map +0 -1
- package/dist/core/scene/nodes/index.d.ts +0 -5
- package/dist/core/scene/nodes/index.d.ts.map +0 -1
- package/dist/core/scene/nodes/index.js +0 -5
- package/dist/core/scene/nodes/index.js.map +0 -1
- package/dist/core/scene/nodes/shapenode.d.ts +0 -76
- package/dist/core/scene/nodes/shapenode.d.ts.map +0 -1
- package/dist/core/scene/nodes/shapenode.js +0 -212
- package/dist/core/scene/nodes/shapenode.js.map +0 -1
- package/dist/core/scene/transform.d.ts +0 -27
- package/dist/core/scene/transform.d.ts.map +0 -1
- package/dist/core/scene/transform.js +0 -52
- package/dist/core/scene/transform.js.map +0 -1
- package/dist/core/serialization/index.d.ts +0 -3
- package/dist/core/serialization/index.d.ts.map +0 -1
- package/dist/core/serialization/index.js +0 -2
- package/dist/core/serialization/index.js.map +0 -1
- package/dist/core/serialization/serializer.d.ts +0 -323
- package/dist/core/serialization/serializer.d.ts.map +0 -1
- package/dist/core/serialization/serializer.js +0 -1173
- package/dist/core/serialization/serializer.js.map +0 -1
- package/dist/core/serialization/types.d.ts +0 -242
- package/dist/core/serialization/types.d.ts.map +0 -1
- package/dist/core/serialization/types.js +0 -1
- package/dist/core/serialization/types.js.map +0 -1
- package/dist/core/timing/clock.d.ts +0 -43
- package/dist/core/timing/clock.d.ts.map +0 -1
- package/dist/core/timing/clock.js +0 -78
- package/dist/core/timing/clock.js.map +0 -1
- package/dist/core/timing/index.d.ts +0 -3
- package/dist/core/timing/index.d.ts.map +0 -1
- package/dist/core/timing/index.js +0 -3
- package/dist/core/timing/index.js.map +0 -1
- package/dist/core/timing/scheduler.d.ts +0 -72
- package/dist/core/timing/scheduler.d.ts.map +0 -1
- package/dist/core/timing/scheduler.js +0 -163
- package/dist/core/timing/scheduler.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/wrapper/react/index.d.ts +0 -5
- package/dist/wrapper/react/index.d.ts.map +0 -1
- package/dist/wrapper/react/index.js +0 -3
- package/dist/wrapper/react/index.js.map +0 -1
- package/dist/wrapper/react/samcan-player.d.ts +0 -26
- package/dist/wrapper/react/samcan-player.d.ts.map +0 -1
- package/dist/wrapper/react/samcan-player.js +0 -19
- package/dist/wrapper/react/samcan-player.js.map +0 -1
- package/dist/wrapper/react/use-samcan-player.d.ts +0 -33
- package/dist/wrapper/react/use-samcan-player.d.ts.map +0 -1
- package/dist/wrapper/react/use-samcan-player.js +0 -65
- package/dist/wrapper/react/use-samcan-player.js.map +0 -1
- package/readme.md +0 -96
|
@@ -0,0 +1,1392 @@
|
|
|
1
|
+
import {
|
|
2
|
+
transform_point_transform
|
|
3
|
+
} from "./chunk-8ynnz57m.js";
|
|
4
|
+
import {
|
|
5
|
+
create_rectangle
|
|
6
|
+
} from "./chunk-35pkr0zs.js";
|
|
7
|
+
import {
|
|
8
|
+
line_cap_round,
|
|
9
|
+
line_cap_square,
|
|
10
|
+
line_join_bevel,
|
|
11
|
+
line_join_round
|
|
12
|
+
} from "./chunk-wa3772hp.js";
|
|
13
|
+
import {
|
|
14
|
+
to_rgba_string_color
|
|
15
|
+
} from "./chunk-v53jprrn.js";
|
|
16
|
+
|
|
17
|
+
// renderer/path.ts
|
|
18
|
+
var path_move = 0;
|
|
19
|
+
var path_line = 1;
|
|
20
|
+
var path_cubic = 2;
|
|
21
|
+
var path_close = 3;
|
|
22
|
+
function create_path() {
|
|
23
|
+
return {
|
|
24
|
+
commands: [],
|
|
25
|
+
bounds: create_rectangle(0, 0, 0, 0)
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function clone_path(p) {
|
|
29
|
+
return {
|
|
30
|
+
commands: [...p.commands],
|
|
31
|
+
bounds: [...p.bounds]
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function move_to_path(p, x, y) {
|
|
35
|
+
const new_commands = [...p.commands, [path_move, x, y]];
|
|
36
|
+
return {
|
|
37
|
+
commands: new_commands,
|
|
38
|
+
bounds: calculate_bounds_of_path(new_commands)
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function line_to_path(p, x, y) {
|
|
42
|
+
const new_commands = [...p.commands, [path_line, x, y]];
|
|
43
|
+
return {
|
|
44
|
+
commands: new_commands,
|
|
45
|
+
bounds: calculate_bounds_of_path(new_commands)
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function cubic_to_path(p, c1x, c1y, c2x, c2y, x, y) {
|
|
49
|
+
const new_commands = [...p.commands, [path_cubic, c1x, c1y, c2x, c2y, x, y]];
|
|
50
|
+
return {
|
|
51
|
+
commands: new_commands,
|
|
52
|
+
bounds: calculate_bounds_of_path(new_commands)
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function close_path(p) {
|
|
56
|
+
const new_commands = [...p.commands, [path_close]];
|
|
57
|
+
return {
|
|
58
|
+
commands: new_commands,
|
|
59
|
+
bounds: p.bounds
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function bounds_of_path(p) {
|
|
63
|
+
return p.bounds;
|
|
64
|
+
}
|
|
65
|
+
function is_empty_path(p) {
|
|
66
|
+
return p.commands.length === 0;
|
|
67
|
+
}
|
|
68
|
+
function command_count_of_path(p) {
|
|
69
|
+
return p.commands.length;
|
|
70
|
+
}
|
|
71
|
+
function transform_path(p, t, out) {
|
|
72
|
+
const transformed_commands = [];
|
|
73
|
+
for (let i = 0;i < p.commands.length; i = i + 1) {
|
|
74
|
+
const cmd = p.commands[i];
|
|
75
|
+
if (cmd !== undefined) {
|
|
76
|
+
transformed_commands.push(transform_command(cmd, t));
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const result = {
|
|
80
|
+
commands: transformed_commands,
|
|
81
|
+
bounds: calculate_bounds_of_path(transformed_commands)
|
|
82
|
+
};
|
|
83
|
+
Object.assign(out, result);
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
function hash_of_path(p) {
|
|
87
|
+
let hash = 0;
|
|
88
|
+
for (let i = 0;i < p.commands.length; i = i + 1) {
|
|
89
|
+
const cmd = p.commands[i];
|
|
90
|
+
if (cmd !== undefined) {
|
|
91
|
+
hash = hash * 31 + hash_command(cmd);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return hash;
|
|
95
|
+
}
|
|
96
|
+
function from_rectangle_path(r) {
|
|
97
|
+
const commands = [
|
|
98
|
+
[path_move, r[0], r[1]],
|
|
99
|
+
[path_line, r[0] + r[2], r[1]],
|
|
100
|
+
[path_line, r[0] + r[2], r[1] + r[3]],
|
|
101
|
+
[path_line, r[0], r[1] + r[3]],
|
|
102
|
+
[path_close]
|
|
103
|
+
];
|
|
104
|
+
return {
|
|
105
|
+
commands,
|
|
106
|
+
bounds: [...r]
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function from_circle_path(c, segments) {
|
|
110
|
+
const commands = [];
|
|
111
|
+
const cx = c[0];
|
|
112
|
+
const cy = c[1];
|
|
113
|
+
const r = c[2];
|
|
114
|
+
if (segments < 3) {
|
|
115
|
+
return create_path();
|
|
116
|
+
}
|
|
117
|
+
const kappa = 4 / 3 * Math.tan(Math.PI / (2 * segments));
|
|
118
|
+
for (let i = 0;i < segments; i = i + 1) {
|
|
119
|
+
const angle1 = 2 * Math.PI * i / segments;
|
|
120
|
+
const angle2 = 2 * Math.PI * (i + 1) / segments;
|
|
121
|
+
const x1 = cx + r * Math.cos(angle1);
|
|
122
|
+
const y1 = cy + r * Math.sin(angle1);
|
|
123
|
+
const x2 = cx + r * Math.cos(angle2);
|
|
124
|
+
const y2 = cy + r * Math.sin(angle2);
|
|
125
|
+
const cos1 = Math.cos(angle1);
|
|
126
|
+
const sin1 = Math.sin(angle1);
|
|
127
|
+
const cos2 = Math.cos(angle2);
|
|
128
|
+
const sin2 = Math.sin(angle2);
|
|
129
|
+
const c1x = x1 - r * kappa * sin1;
|
|
130
|
+
const c1y = y1 + r * kappa * cos1;
|
|
131
|
+
const c2x = x2 + r * kappa * sin2;
|
|
132
|
+
const c2y = y2 - r * kappa * cos2;
|
|
133
|
+
if (i === 0) {
|
|
134
|
+
commands.push([path_move, x1, y1]);
|
|
135
|
+
}
|
|
136
|
+
commands.push([path_cubic, c1x, c1y, c2x, c2y, x2, y2]);
|
|
137
|
+
}
|
|
138
|
+
commands.push([path_close]);
|
|
139
|
+
const bounds = create_rectangle(cx - r, cy - r, r * 2, r * 2);
|
|
140
|
+
return {
|
|
141
|
+
commands,
|
|
142
|
+
bounds
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
function from_polyline_path(points, closed) {
|
|
146
|
+
if (points.length === 0) {
|
|
147
|
+
return create_path();
|
|
148
|
+
}
|
|
149
|
+
const commands = [];
|
|
150
|
+
const first_point = points[0];
|
|
151
|
+
if (first_point !== undefined) {
|
|
152
|
+
commands.push([path_move, first_point[0], first_point[1]]);
|
|
153
|
+
}
|
|
154
|
+
for (let i = 1;i < points.length; i = i + 1) {
|
|
155
|
+
const point = points[i];
|
|
156
|
+
if (point !== undefined) {
|
|
157
|
+
commands.push([path_line, point[0], point[1]]);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (closed && points.length > 2) {
|
|
161
|
+
commands.push([path_close]);
|
|
162
|
+
}
|
|
163
|
+
return {
|
|
164
|
+
commands,
|
|
165
|
+
bounds: calculate_bounds_of_path(commands)
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
function calculate_bounds_of_path(commands) {
|
|
169
|
+
if (commands.length === 0) {
|
|
170
|
+
return create_rectangle(0, 0, 0, 0);
|
|
171
|
+
}
|
|
172
|
+
let min_x = Infinity;
|
|
173
|
+
let min_y = Infinity;
|
|
174
|
+
let max_x = -Infinity;
|
|
175
|
+
let max_y = -Infinity;
|
|
176
|
+
for (let i = 0;i < commands.length; i = i + 1) {
|
|
177
|
+
const cmd = commands[i];
|
|
178
|
+
if (cmd === undefined) {
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
const type = cmd[0];
|
|
182
|
+
if (type === path_move || type === path_line) {
|
|
183
|
+
const cmd_move_line = cmd;
|
|
184
|
+
const x = cmd_move_line[1];
|
|
185
|
+
const y = cmd_move_line[2];
|
|
186
|
+
min_x = Math.min(min_x, x);
|
|
187
|
+
min_y = Math.min(min_y, y);
|
|
188
|
+
max_x = Math.max(max_x, x);
|
|
189
|
+
max_y = Math.max(max_y, y);
|
|
190
|
+
} else if (type === path_cubic) {
|
|
191
|
+
const cmd_cubic = cmd;
|
|
192
|
+
const c1x = cmd_cubic[1];
|
|
193
|
+
const c1y = cmd_cubic[2];
|
|
194
|
+
const c2x = cmd_cubic[3];
|
|
195
|
+
const c2y = cmd_cubic[4];
|
|
196
|
+
const x = cmd_cubic[5];
|
|
197
|
+
const y = cmd_cubic[6];
|
|
198
|
+
min_x = Math.min(min_x, c1x, c2x, x);
|
|
199
|
+
min_y = Math.min(min_y, c1y, c2y, y);
|
|
200
|
+
max_x = Math.max(max_x, c1x, c2x, x);
|
|
201
|
+
max_y = Math.max(max_y, c1y, c2y, y);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
if (min_x === Infinity) {
|
|
205
|
+
return create_rectangle(0, 0, 0, 0);
|
|
206
|
+
}
|
|
207
|
+
return create_rectangle(min_x, min_y, max_x - min_x, max_y - min_y);
|
|
208
|
+
}
|
|
209
|
+
function transform_command(cmd, t) {
|
|
210
|
+
const type = cmd[0];
|
|
211
|
+
if (type === path_move || type === path_line) {
|
|
212
|
+
const cmd_move_line = cmd;
|
|
213
|
+
const p1 = [cmd_move_line[1], cmd_move_line[2]];
|
|
214
|
+
const out1 = [0, 0];
|
|
215
|
+
transform_point_transform(t, p1, out1);
|
|
216
|
+
return [type, out1[0], out1[1]];
|
|
217
|
+
} else if (type === path_cubic) {
|
|
218
|
+
const cmd_cubic = cmd;
|
|
219
|
+
const p1 = [cmd_cubic[1], cmd_cubic[2]];
|
|
220
|
+
const p2 = [cmd_cubic[3], cmd_cubic[4]];
|
|
221
|
+
const p3 = [cmd_cubic[5], cmd_cubic[6]];
|
|
222
|
+
const out1 = [0, 0];
|
|
223
|
+
const out2 = [0, 0];
|
|
224
|
+
const out3 = [0, 0];
|
|
225
|
+
transform_point_transform(t, p1, out1);
|
|
226
|
+
transform_point_transform(t, p2, out2);
|
|
227
|
+
transform_point_transform(t, p3, out3);
|
|
228
|
+
return [path_cubic, out1[0], out1[1], out2[0], out2[1], out3[0], out3[1]];
|
|
229
|
+
} else {
|
|
230
|
+
return cmd;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
function hash_command(cmd) {
|
|
234
|
+
const type = cmd[0];
|
|
235
|
+
let hash = type * 31;
|
|
236
|
+
if (type === path_move || type === path_line) {
|
|
237
|
+
const cmd_move_line = cmd;
|
|
238
|
+
hash = hash * 31 + Math.floor(cmd_move_line[1] * 1000);
|
|
239
|
+
hash = hash * 31 + Math.floor(cmd_move_line[2] * 1000);
|
|
240
|
+
} else if (type === path_cubic) {
|
|
241
|
+
const cmd_cubic = cmd;
|
|
242
|
+
hash = hash * 31 + Math.floor(cmd_cubic[1] * 1000);
|
|
243
|
+
hash = hash * 31 + Math.floor(cmd_cubic[2] * 1000);
|
|
244
|
+
hash = hash * 31 + Math.floor(cmd_cubic[3] * 1000);
|
|
245
|
+
hash = hash * 31 + Math.floor(cmd_cubic[4] * 1000);
|
|
246
|
+
hash = hash * 31 + Math.floor(cmd_cubic[5] * 1000);
|
|
247
|
+
hash = hash * 31 + Math.floor(cmd_cubic[6] * 1000);
|
|
248
|
+
}
|
|
249
|
+
return hash;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// renderer/webgl/lifecycle.ts
|
|
253
|
+
function begin_frame_webgl(state) {
|
|
254
|
+
state.vertex_count = 0;
|
|
255
|
+
state.index_count = 0;
|
|
256
|
+
state.scissor_enabled = false;
|
|
257
|
+
state.gl.disable(state.gl.SCISSOR_TEST);
|
|
258
|
+
}
|
|
259
|
+
function end_frame_webgl(state) {
|
|
260
|
+
if (state.index_count > 0) {
|
|
261
|
+
flush_webgl(state);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
function flush_webgl(state) {
|
|
265
|
+
if (state.index_count === 0) {
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
const gl = state.gl;
|
|
269
|
+
gl.useProgram(state.program);
|
|
270
|
+
gl.uniformMatrix3fv(state.program_locations.transform, false, state.current_transform);
|
|
271
|
+
gl.uniform2f(state.program_locations.resolution, state.width, state.height);
|
|
272
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, state.position_buffer);
|
|
273
|
+
gl.bufferData(gl.ARRAY_BUFFER, state.vertices.subarray(0, state.vertex_count * 2), gl.DYNAMIC_DRAW);
|
|
274
|
+
gl.enableVertexAttribArray(state.program_locations.position);
|
|
275
|
+
gl.vertexAttribPointer(state.program_locations.position, 2, gl.FLOAT, false, 0, 0);
|
|
276
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, state.color_buffer);
|
|
277
|
+
gl.bufferData(gl.ARRAY_BUFFER, state.colors.subarray(0, state.vertex_count * 4), gl.DYNAMIC_DRAW);
|
|
278
|
+
gl.enableVertexAttribArray(state.program_locations.color);
|
|
279
|
+
gl.vertexAttribPointer(state.program_locations.color, 4, gl.FLOAT, false, 0, 0);
|
|
280
|
+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, state.index_buffer);
|
|
281
|
+
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, state.indices.subarray(0, state.index_count), gl.DYNAMIC_DRAW);
|
|
282
|
+
gl.drawElements(gl.TRIANGLES, state.index_count, gl.UNSIGNED_SHORT, 0);
|
|
283
|
+
state.vertex_count = 0;
|
|
284
|
+
state.index_count = 0;
|
|
285
|
+
}
|
|
286
|
+
function clip_rectangle_webgl(state, r) {
|
|
287
|
+
if (state.index_count > 0) {
|
|
288
|
+
flush_webgl(state);
|
|
289
|
+
}
|
|
290
|
+
state.scissor_enabled = true;
|
|
291
|
+
state.gl.enable(state.gl.SCISSOR_TEST);
|
|
292
|
+
state.gl.scissor(Math.floor(r[0] * state.pixel_ratio), Math.floor((state.height - r[1] - r[3]) * state.pixel_ratio), Math.ceil(r[2] * state.pixel_ratio), Math.ceil(r[3] * state.pixel_ratio));
|
|
293
|
+
}
|
|
294
|
+
function clear_webgl(state, c) {
|
|
295
|
+
if (state.index_count > 0) {
|
|
296
|
+
flush_webgl(state);
|
|
297
|
+
}
|
|
298
|
+
state.gl.clearColor(c[0], c[1], c[2], c[3]);
|
|
299
|
+
state.gl.clear(state.gl.COLOR_BUFFER_BIT);
|
|
300
|
+
}
|
|
301
|
+
function resize_webgl(state, width, height, pixel_ratio) {
|
|
302
|
+
state.width = width;
|
|
303
|
+
state.height = height;
|
|
304
|
+
state.pixel_ratio = pixel_ratio;
|
|
305
|
+
state.canvas.width = width * pixel_ratio;
|
|
306
|
+
state.canvas.height = height * pixel_ratio;
|
|
307
|
+
state.canvas.style.width = width + "px";
|
|
308
|
+
state.canvas.style.height = height + "px";
|
|
309
|
+
state.gl.viewport(0, 0, state.canvas.width, state.canvas.height);
|
|
310
|
+
}
|
|
311
|
+
function dispose_webgl(state) {
|
|
312
|
+
state.fill_cache.clear();
|
|
313
|
+
state.stroke_cache.clear();
|
|
314
|
+
for (const texture of state.texture_cache.values()) {
|
|
315
|
+
state.gl.deleteTexture(texture.texture);
|
|
316
|
+
}
|
|
317
|
+
state.texture_cache.clear();
|
|
318
|
+
state.gl.deleteBuffer(state.position_buffer);
|
|
319
|
+
state.gl.deleteBuffer(state.color_buffer);
|
|
320
|
+
state.gl.deleteBuffer(state.texcoord_buffer);
|
|
321
|
+
state.gl.deleteBuffer(state.index_buffer);
|
|
322
|
+
state.gl.deleteProgram(state.program);
|
|
323
|
+
state.gl.deleteProgram(state.texture_program);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// renderer/webgl/batches.ts
|
|
327
|
+
function draw_cached_triangles_webgl(state, cached, color, alpha) {
|
|
328
|
+
if (cached.index_count === 0) {
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
const added_vertices = new Int32Array(cached.vertex_count);
|
|
332
|
+
for (let i = 0;i < cached.vertex_count; i = i + 1) {
|
|
333
|
+
added_vertices[i] = -1;
|
|
334
|
+
}
|
|
335
|
+
for (let tri = 0;tri < cached.index_count; tri = tri + 3) {
|
|
336
|
+
const i0 = cached.indices[tri];
|
|
337
|
+
const i1 = cached.indices[tri + 1];
|
|
338
|
+
const i2 = cached.indices[tri + 2];
|
|
339
|
+
if (i0 === undefined || i1 === undefined || i2 === undefined) {
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
if (state.vertex_count + 3 > state.max_vertices || state.index_count + 3 > state.max_indices) {
|
|
343
|
+
flush_webgl(state);
|
|
344
|
+
for (let i = 0;i < cached.vertex_count; i = i + 1) {
|
|
345
|
+
added_vertices[i] = -1;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
write_triangle_webgl(state, cached.vertices, added_vertices, [i0, i1, i2], color, alpha);
|
|
349
|
+
const v0 = added_vertices[i0];
|
|
350
|
+
const v1 = added_vertices[i1];
|
|
351
|
+
const v2 = added_vertices[i2];
|
|
352
|
+
if (v0 !== -1 && v0 !== undefined && v1 !== -1 && v1 !== undefined && v2 !== -1 && v2 !== undefined) {
|
|
353
|
+
state.indices[state.index_count] = v0;
|
|
354
|
+
state.indices[state.index_count + 1] = v1;
|
|
355
|
+
state.indices[state.index_count + 2] = v2;
|
|
356
|
+
state.index_count = state.index_count + 3;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
function write_triangle_webgl(state, vertices, added_vertices, indices, color, alpha) {
|
|
361
|
+
for (let i = 0;i < indices.length; i = i + 1) {
|
|
362
|
+
const idx = indices[i];
|
|
363
|
+
if (idx === undefined || added_vertices[idx] !== -1) {
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
const vx = vertices[idx * 2];
|
|
367
|
+
const vy = vertices[idx * 2 + 1];
|
|
368
|
+
if (vx !== undefined && vy !== undefined) {
|
|
369
|
+
state.vertices[state.vertex_count * 2] = vx;
|
|
370
|
+
state.vertices[state.vertex_count * 2 + 1] = vy;
|
|
371
|
+
state.colors[state.vertex_count * 4] = color[0];
|
|
372
|
+
state.colors[state.vertex_count * 4 + 1] = color[1];
|
|
373
|
+
state.colors[state.vertex_count * 4 + 2] = color[2];
|
|
374
|
+
state.colors[state.vertex_count * 4 + 3] = color[3] * alpha;
|
|
375
|
+
added_vertices[idx] = state.vertex_count;
|
|
376
|
+
state.vertex_count = state.vertex_count + 1;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// renderer/webgl/triangles.ts
|
|
382
|
+
function triangulate_polygon_earcut(vertices) {
|
|
383
|
+
if (vertices.length < 6) {
|
|
384
|
+
return [];
|
|
385
|
+
}
|
|
386
|
+
const n = vertices.length / 2;
|
|
387
|
+
if (n === 3) {
|
|
388
|
+
return [0, 1, 2];
|
|
389
|
+
}
|
|
390
|
+
let head = null;
|
|
391
|
+
let last = null;
|
|
392
|
+
for (let i = 0;i < n; i = i + 1) {
|
|
393
|
+
const x = vertices[i * 2];
|
|
394
|
+
const y = vertices[i * 2 + 1];
|
|
395
|
+
if (x !== undefined && y !== undefined) {
|
|
396
|
+
const node = { i, x, y, prev: last, next: null };
|
|
397
|
+
if (last !== null) {
|
|
398
|
+
last.next = node;
|
|
399
|
+
} else {
|
|
400
|
+
head = node;
|
|
401
|
+
}
|
|
402
|
+
last = node;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
if (head !== null && last !== null) {
|
|
406
|
+
head.prev = last;
|
|
407
|
+
last.next = head;
|
|
408
|
+
}
|
|
409
|
+
const indices = [];
|
|
410
|
+
let ear = head;
|
|
411
|
+
let count = n * 2;
|
|
412
|
+
while (count > 0 && ear !== null && ear.next !== ear.prev) {
|
|
413
|
+
count = count - 1;
|
|
414
|
+
const prev = ear.prev;
|
|
415
|
+
const next = ear.next;
|
|
416
|
+
if (prev !== null && next !== null && is_ear(prev, ear, next)) {
|
|
417
|
+
indices.push(prev.i, ear.i, next.i);
|
|
418
|
+
if (prev.next === next) {
|
|
419
|
+
break;
|
|
420
|
+
}
|
|
421
|
+
prev.next = next;
|
|
422
|
+
next.prev = prev;
|
|
423
|
+
ear = next;
|
|
424
|
+
} else {
|
|
425
|
+
ear = next;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
return indices;
|
|
429
|
+
}
|
|
430
|
+
function is_ear(a, b, c) {
|
|
431
|
+
const area = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
|
|
432
|
+
if (area >= 0) {
|
|
433
|
+
return false;
|
|
434
|
+
}
|
|
435
|
+
let p = c.next;
|
|
436
|
+
while (p !== null && p !== a) {
|
|
437
|
+
if (point_in_triangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y)) {
|
|
438
|
+
return false;
|
|
439
|
+
}
|
|
440
|
+
p = p.next;
|
|
441
|
+
}
|
|
442
|
+
return true;
|
|
443
|
+
}
|
|
444
|
+
function point_in_triangle(ax, ay, bx, by, cx, cy, px, py) {
|
|
445
|
+
const denom = (by - cy) * (ax - cx) + (cx - bx) * (ay - cy);
|
|
446
|
+
if (denom === 0) {
|
|
447
|
+
return false;
|
|
448
|
+
}
|
|
449
|
+
const a = ((by - cy) * (px - cx) + (cx - bx) * (py - cy)) / denom;
|
|
450
|
+
const b = ((cy - ay) * (px - cx) + (ax - cx) * (py - cy)) / denom;
|
|
451
|
+
const c = 1 - a - b;
|
|
452
|
+
return a >= 0 && b >= 0 && c >= 0;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// renderer/webgl/fills.ts
|
|
456
|
+
function cache_path_fill_webgl(p) {
|
|
457
|
+
const fill_vertices = [];
|
|
458
|
+
const fill_colors = [];
|
|
459
|
+
const fill_indices = [];
|
|
460
|
+
let current_x = 0;
|
|
461
|
+
let current_y = 0;
|
|
462
|
+
let start_x = 0;
|
|
463
|
+
let start_y = 0;
|
|
464
|
+
for (let i = 0;i < p.commands.length; i = i + 1) {
|
|
465
|
+
const cmd = p.commands[i];
|
|
466
|
+
if (cmd === undefined) {
|
|
467
|
+
continue;
|
|
468
|
+
}
|
|
469
|
+
const type = cmd[0];
|
|
470
|
+
if (type === path_move) {
|
|
471
|
+
current_x = cmd[1];
|
|
472
|
+
current_y = cmd[2];
|
|
473
|
+
start_x = current_x;
|
|
474
|
+
start_y = current_y;
|
|
475
|
+
} else if (type === path_line) {
|
|
476
|
+
fill_vertices.push(current_x, current_y);
|
|
477
|
+
current_x = cmd[1];
|
|
478
|
+
current_y = cmd[2];
|
|
479
|
+
} else if (type === path_cubic) {
|
|
480
|
+
const steps = 8;
|
|
481
|
+
let prev_x = current_x;
|
|
482
|
+
let prev_y = current_y;
|
|
483
|
+
for (let step = 1;step <= steps; step = step + 1) {
|
|
484
|
+
const t = step / steps;
|
|
485
|
+
const inv_t = 1 - t;
|
|
486
|
+
const t2 = t * t;
|
|
487
|
+
const inv_t2 = inv_t * inv_t;
|
|
488
|
+
const t3 = t2 * t;
|
|
489
|
+
const inv_t3 = inv_t2 * inv_t;
|
|
490
|
+
const x = inv_t3 * current_x + 3 * inv_t2 * t * cmd[1] + 3 * inv_t * t2 * cmd[3] + t3 * cmd[5];
|
|
491
|
+
const y = inv_t3 * current_y + 3 * inv_t2 * t * cmd[2] + 3 * inv_t * t2 * cmd[4] + t3 * cmd[6];
|
|
492
|
+
fill_vertices.push(prev_x, prev_y);
|
|
493
|
+
prev_x = x;
|
|
494
|
+
prev_y = y;
|
|
495
|
+
}
|
|
496
|
+
current_x = cmd[5];
|
|
497
|
+
current_y = cmd[6];
|
|
498
|
+
} else if (type === path_close) {
|
|
499
|
+
if (current_x !== start_x || current_y !== start_y) {
|
|
500
|
+
fill_vertices.push(current_x, current_y);
|
|
501
|
+
}
|
|
502
|
+
fill_vertices.push(start_x, start_y);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
if (fill_vertices.length > 0 && (current_x !== start_x || current_y !== start_y)) {
|
|
506
|
+
fill_vertices.push(start_x, start_y);
|
|
507
|
+
}
|
|
508
|
+
const fill_tris = triangulate_polygon_earcut(fill_vertices);
|
|
509
|
+
for (let i = 0;i < fill_tris.length; i = i + 1) {
|
|
510
|
+
const tri_index = fill_tris[i];
|
|
511
|
+
if (tri_index !== undefined) {
|
|
512
|
+
fill_indices.push(tri_index);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
for (let i = 0;i < fill_vertices.length / 2; i = i + 1) {
|
|
516
|
+
fill_colors.push(1, 1, 1, 1);
|
|
517
|
+
}
|
|
518
|
+
return {
|
|
519
|
+
vertices: new Float32Array(fill_vertices),
|
|
520
|
+
colors: new Float32Array(fill_colors),
|
|
521
|
+
indices: new Uint16Array(fill_indices),
|
|
522
|
+
vertex_count: fill_vertices.length / 2,
|
|
523
|
+
index_count: fill_indices.length,
|
|
524
|
+
path_commands: p.commands
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
function draw_cached_path_fill_webgl(state, cached, color, alpha) {
|
|
528
|
+
draw_cached_triangles_webgl(state, cached, color, alpha);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
// renderer/webgl/strokes.ts
|
|
532
|
+
function cache_path_stroke_webgl(p, stroke_width) {
|
|
533
|
+
const stroke_vertices = [];
|
|
534
|
+
const stroke_colors = [];
|
|
535
|
+
const stroke_indices = [];
|
|
536
|
+
let current_x = 0;
|
|
537
|
+
let current_y = 0;
|
|
538
|
+
let start_x = 0;
|
|
539
|
+
let start_y = 0;
|
|
540
|
+
let vertex_offset = 0;
|
|
541
|
+
for (let i = 0;i < p.commands.length; i = i + 1) {
|
|
542
|
+
const cmd = p.commands[i];
|
|
543
|
+
if (cmd === undefined) {
|
|
544
|
+
continue;
|
|
545
|
+
}
|
|
546
|
+
const type = cmd[0];
|
|
547
|
+
if (type === path_move) {
|
|
548
|
+
current_x = cmd[1];
|
|
549
|
+
current_y = cmd[2];
|
|
550
|
+
start_x = current_x;
|
|
551
|
+
start_y = current_y;
|
|
552
|
+
} else if (type === path_line) {
|
|
553
|
+
add_segment_webgl(stroke_vertices, stroke_indices, vertex_offset, current_x, current_y, cmd[1], cmd[2], stroke_width / 2);
|
|
554
|
+
vertex_offset = vertex_offset + 4;
|
|
555
|
+
current_x = cmd[1];
|
|
556
|
+
current_y = cmd[2];
|
|
557
|
+
} else if (type === path_cubic) {
|
|
558
|
+
const steps = 8;
|
|
559
|
+
let prev_x = current_x;
|
|
560
|
+
let prev_y = current_y;
|
|
561
|
+
for (let step = 1;step <= steps; step = step + 1) {
|
|
562
|
+
const t = step / steps;
|
|
563
|
+
const inv_t = 1 - t;
|
|
564
|
+
const t2 = t * t;
|
|
565
|
+
const inv_t2 = inv_t * inv_t;
|
|
566
|
+
const t3 = t2 * t;
|
|
567
|
+
const inv_t3 = inv_t2 * inv_t;
|
|
568
|
+
const x = inv_t3 * current_x + 3 * inv_t2 * t * cmd[1] + 3 * inv_t * t2 * cmd[3] + t3 * cmd[5];
|
|
569
|
+
const y = inv_t3 * current_y + 3 * inv_t2 * t * cmd[2] + 3 * inv_t * t2 * cmd[4] + t3 * cmd[6];
|
|
570
|
+
add_segment_webgl(stroke_vertices, stroke_indices, vertex_offset, prev_x, prev_y, x, y, stroke_width / 2);
|
|
571
|
+
vertex_offset = vertex_offset + 4;
|
|
572
|
+
prev_x = x;
|
|
573
|
+
prev_y = y;
|
|
574
|
+
}
|
|
575
|
+
current_x = cmd[5];
|
|
576
|
+
current_y = cmd[6];
|
|
577
|
+
} else if (type === path_close) {
|
|
578
|
+
if (current_x !== start_x || current_y !== start_y) {
|
|
579
|
+
add_segment_webgl(stroke_vertices, stroke_indices, vertex_offset, current_x, current_y, start_x, start_y, stroke_width / 2);
|
|
580
|
+
vertex_offset = vertex_offset + 4;
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
for (let i = 0;i < stroke_vertices.length / 2; i = i + 1) {
|
|
585
|
+
stroke_colors.push(1, 1, 1, 1);
|
|
586
|
+
}
|
|
587
|
+
return {
|
|
588
|
+
vertices: new Float32Array(stroke_vertices),
|
|
589
|
+
colors: new Float32Array(stroke_colors),
|
|
590
|
+
indices: new Uint16Array(stroke_indices),
|
|
591
|
+
vertex_count: stroke_vertices.length / 2,
|
|
592
|
+
index_count: stroke_indices.length,
|
|
593
|
+
stroke_width,
|
|
594
|
+
path_commands: p.commands
|
|
595
|
+
};
|
|
596
|
+
}
|
|
597
|
+
function draw_cached_path_stroke_webgl(state, cached, color, alpha) {
|
|
598
|
+
draw_cached_triangles_webgl(state, cached, color, alpha);
|
|
599
|
+
}
|
|
600
|
+
function add_segment_webgl(vertices, indices, base_index, x1, y1, x2, y2, half_width) {
|
|
601
|
+
add_line_vertices(vertices, x1, y1, x2, y2, half_width);
|
|
602
|
+
indices.push(base_index, base_index + 1, base_index + 2, base_index + 1, base_index + 3, base_index + 2);
|
|
603
|
+
}
|
|
604
|
+
function add_line_vertices(vertices, x1, y1, x2, y2, half_width) {
|
|
605
|
+
const dx = x2 - x1;
|
|
606
|
+
const dy = y2 - y1;
|
|
607
|
+
const len = Math.sqrt(dx * dx + dy * dy);
|
|
608
|
+
if (len === 0) {
|
|
609
|
+
vertices.push(x1 - half_width, y1 - half_width, x1 + half_width, y1 - half_width, x1 - half_width, y1 + half_width, x1 + half_width, y1 + half_width);
|
|
610
|
+
return;
|
|
611
|
+
}
|
|
612
|
+
const nx = -dy / len * half_width;
|
|
613
|
+
const ny = dx / len * half_width;
|
|
614
|
+
vertices.push(x1 + nx, y1 + ny, x2 + nx, y2 + ny, x1 - nx, y1 - ny, x2 - nx, y2 - ny);
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
// renderer/webgl/paths.ts
|
|
618
|
+
function draw_path_webgl(state, p, s) {
|
|
619
|
+
if (s.fill !== null) {
|
|
620
|
+
const hash = hash_of_path(p);
|
|
621
|
+
let fill_cached = state.fill_cache.get(hash);
|
|
622
|
+
if (fill_cached === undefined || !paths_equal(fill_cached.path_commands, p.commands)) {
|
|
623
|
+
fill_cached = cache_path_fill_webgl(p);
|
|
624
|
+
update_cache_webgl(state.fill_cache, state.cache_max_size, hash, fill_cached);
|
|
625
|
+
}
|
|
626
|
+
draw_cached_path_fill_webgl(state, fill_cached, s.fill, s.alpha);
|
|
627
|
+
}
|
|
628
|
+
if (s.stroke !== null && s.stroke_width > 0) {
|
|
629
|
+
const hash = hash_of_path(p) * 31 + s.stroke_width;
|
|
630
|
+
let stroke_cached = state.stroke_cache.get(hash);
|
|
631
|
+
if (stroke_cached === undefined || stroke_cached.stroke_width !== s.stroke_width || !paths_equal(stroke_cached.path_commands, p.commands)) {
|
|
632
|
+
stroke_cached = cache_path_stroke_webgl(p, s.stroke_width);
|
|
633
|
+
update_cache_webgl(state.stroke_cache, state.cache_max_size, hash, stroke_cached);
|
|
634
|
+
}
|
|
635
|
+
draw_cached_path_stroke_webgl(state, stroke_cached, s.stroke, s.alpha);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
function update_cache_webgl(cache, max_size, key, value) {
|
|
639
|
+
if (cache.size >= max_size) {
|
|
640
|
+
const first_key = cache.keys().next().value;
|
|
641
|
+
if (first_key !== undefined) {
|
|
642
|
+
cache.delete(first_key);
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
cache.set(key, value);
|
|
646
|
+
}
|
|
647
|
+
function paths_equal(a, b) {
|
|
648
|
+
if (a.length !== b.length) {
|
|
649
|
+
return false;
|
|
650
|
+
}
|
|
651
|
+
for (let i = 0;i < a.length; i = i + 1) {
|
|
652
|
+
const cmd_a = a[i];
|
|
653
|
+
const cmd_b = b[i];
|
|
654
|
+
if (cmd_a === undefined || cmd_b === undefined) {
|
|
655
|
+
return false;
|
|
656
|
+
}
|
|
657
|
+
if (cmd_a.length !== cmd_b.length) {
|
|
658
|
+
return false;
|
|
659
|
+
}
|
|
660
|
+
for (let j = 0;j < cmd_a.length; j = j + 1) {
|
|
661
|
+
if (cmd_a[j] !== cmd_b[j]) {
|
|
662
|
+
return false;
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
return true;
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
// renderer/webgl/shaders.ts
|
|
670
|
+
var vertex_shader_source = `
|
|
671
|
+
attribute vec2 a_position;
|
|
672
|
+
attribute vec4 a_color;
|
|
673
|
+
uniform mat3 u_transform;
|
|
674
|
+
uniform vec2 u_resolution;
|
|
675
|
+
varying vec4 v_color;
|
|
676
|
+
|
|
677
|
+
void main() {
|
|
678
|
+
vec2 position = (u_transform * vec3(a_position, 1.0)).xy;
|
|
679
|
+
vec2 clip_space = ((position / u_resolution) * 2.0) - 1.0;
|
|
680
|
+
gl_Position = vec4(clip_space * vec2(1.0, -1.0), 0.0, 1.0);
|
|
681
|
+
v_color = a_color;
|
|
682
|
+
}
|
|
683
|
+
`;
|
|
684
|
+
var fragment_shader_source = `
|
|
685
|
+
precision mediump float;
|
|
686
|
+
varying vec4 v_color;
|
|
687
|
+
|
|
688
|
+
void main() {
|
|
689
|
+
gl_FragColor = v_color;
|
|
690
|
+
}
|
|
691
|
+
`;
|
|
692
|
+
var texture_vertex_shader_source = `
|
|
693
|
+
attribute vec2 a_position;
|
|
694
|
+
attribute vec2 a_texcoord;
|
|
695
|
+
uniform mat3 u_transform;
|
|
696
|
+
uniform vec2 u_resolution;
|
|
697
|
+
varying vec2 v_texcoord;
|
|
698
|
+
|
|
699
|
+
void main() {
|
|
700
|
+
vec2 position = (u_transform * vec3(a_position, 1.0)).xy;
|
|
701
|
+
vec2 clip_space = ((position / u_resolution) * 2.0) - 1.0;
|
|
702
|
+
gl_Position = vec4(clip_space * vec2(1.0, -1.0), 0.0, 1.0);
|
|
703
|
+
v_texcoord = a_texcoord;
|
|
704
|
+
}
|
|
705
|
+
`;
|
|
706
|
+
var texture_fragment_shader_source = `
|
|
707
|
+
precision mediump float;
|
|
708
|
+
uniform sampler2D u_image;
|
|
709
|
+
uniform float u_alpha;
|
|
710
|
+
varying vec2 v_texcoord;
|
|
711
|
+
|
|
712
|
+
void main() {
|
|
713
|
+
gl_FragColor = texture2D(u_image, v_texcoord);
|
|
714
|
+
gl_FragColor.a *= u_alpha;
|
|
715
|
+
}
|
|
716
|
+
`;
|
|
717
|
+
function create_color_program_webgl(gl) {
|
|
718
|
+
const program = create_shader_program(gl, vertex_shader_source, fragment_shader_source);
|
|
719
|
+
if (program === null) {
|
|
720
|
+
return null;
|
|
721
|
+
}
|
|
722
|
+
const position = gl.getAttribLocation(program, "a_position");
|
|
723
|
+
const color = gl.getAttribLocation(program, "a_color");
|
|
724
|
+
const transform = gl.getUniformLocation(program, "u_transform");
|
|
725
|
+
const resolution = gl.getUniformLocation(program, "u_resolution");
|
|
726
|
+
if (position < 0 || color < 0 || transform === null || resolution === null) {
|
|
727
|
+
gl.deleteProgram(program);
|
|
728
|
+
return null;
|
|
729
|
+
}
|
|
730
|
+
return {
|
|
731
|
+
program,
|
|
732
|
+
locations: { position, color, transform, resolution }
|
|
733
|
+
};
|
|
734
|
+
}
|
|
735
|
+
function create_texture_program_webgl(gl) {
|
|
736
|
+
const program = create_shader_program(gl, texture_vertex_shader_source, texture_fragment_shader_source);
|
|
737
|
+
if (program === null) {
|
|
738
|
+
return null;
|
|
739
|
+
}
|
|
740
|
+
const position = gl.getAttribLocation(program, "a_position");
|
|
741
|
+
const texcoord = gl.getAttribLocation(program, "a_texcoord");
|
|
742
|
+
const transform = gl.getUniformLocation(program, "u_transform");
|
|
743
|
+
const resolution = gl.getUniformLocation(program, "u_resolution");
|
|
744
|
+
const alpha = gl.getUniformLocation(program, "u_alpha");
|
|
745
|
+
const sampler = gl.getUniformLocation(program, "u_image");
|
|
746
|
+
if (position < 0 || texcoord < 0 || transform === null || resolution === null || alpha === null || sampler === null) {
|
|
747
|
+
gl.deleteProgram(program);
|
|
748
|
+
return null;
|
|
749
|
+
}
|
|
750
|
+
return {
|
|
751
|
+
program,
|
|
752
|
+
locations: { position, texcoord, transform, resolution, alpha, sampler }
|
|
753
|
+
};
|
|
754
|
+
}
|
|
755
|
+
function create_shader_program(gl, vertex_source, fragment_source) {
|
|
756
|
+
const vertex_shader = compile_shader(gl, vertex_source, gl.VERTEX_SHADER);
|
|
757
|
+
const fragment_shader = compile_shader(gl, fragment_source, gl.FRAGMENT_SHADER);
|
|
758
|
+
if (vertex_shader === null || fragment_shader === null) {
|
|
759
|
+
return null;
|
|
760
|
+
}
|
|
761
|
+
const program = gl.createProgram();
|
|
762
|
+
if (program === null) {
|
|
763
|
+
return null;
|
|
764
|
+
}
|
|
765
|
+
gl.attachShader(program, vertex_shader);
|
|
766
|
+
gl.attachShader(program, fragment_shader);
|
|
767
|
+
gl.linkProgram(program);
|
|
768
|
+
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
|
769
|
+
gl.deleteProgram(program);
|
|
770
|
+
return null;
|
|
771
|
+
}
|
|
772
|
+
return program;
|
|
773
|
+
}
|
|
774
|
+
function compile_shader(gl, source, type) {
|
|
775
|
+
const shader = gl.createShader(type);
|
|
776
|
+
if (shader === null) {
|
|
777
|
+
return null;
|
|
778
|
+
}
|
|
779
|
+
gl.shaderSource(shader, source);
|
|
780
|
+
gl.compileShader(shader);
|
|
781
|
+
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
782
|
+
gl.deleteShader(shader);
|
|
783
|
+
return null;
|
|
784
|
+
}
|
|
785
|
+
return shader;
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
// renderer/webgl/shapes.ts
|
|
789
|
+
function draw_rectangle_webgl(state, r, s) {
|
|
790
|
+
const x = r[0];
|
|
791
|
+
const y = r[1];
|
|
792
|
+
const w = r[2];
|
|
793
|
+
const h = r[3];
|
|
794
|
+
if (s.fill !== null) {
|
|
795
|
+
add_quad_webgl(state, x, y, x + w, y, x + w, y + h, x, y + h, s.fill, s.alpha);
|
|
796
|
+
}
|
|
797
|
+
if (s.stroke !== null && s.stroke_width > 0) {
|
|
798
|
+
const sw = s.stroke_width;
|
|
799
|
+
add_quad_webgl(state, x - sw, y - sw, x + w + sw, y - sw, x + w + sw, y, x - sw, y, s.stroke, s.alpha);
|
|
800
|
+
add_quad_webgl(state, x + w, y, x + w + sw, y, x + w + sw, y + h, x + w, y + h, s.stroke, s.alpha);
|
|
801
|
+
add_quad_webgl(state, x - sw, y + h, x + w + sw, y + h, x + w + sw, y + h + sw, x - sw, y + h + sw, s.stroke, s.alpha);
|
|
802
|
+
add_quad_webgl(state, x - sw, y, x, y, x, y + h, x - sw, y + h, s.stroke, s.alpha);
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
function draw_circle_webgl(state, c, s) {
|
|
806
|
+
const cx = c[0];
|
|
807
|
+
const cy = c[1];
|
|
808
|
+
const r = c[2];
|
|
809
|
+
const segments = 32;
|
|
810
|
+
if (s.fill !== null) {
|
|
811
|
+
const base_vertex = state.vertex_count;
|
|
812
|
+
write_vertex_webgl(state, cx, cy, s.fill, s.alpha);
|
|
813
|
+
for (let i = 0;i <= segments; i = i + 1) {
|
|
814
|
+
const angle = i / segments * Math.PI * 2;
|
|
815
|
+
write_vertex_webgl(state, cx + Math.cos(angle) * r, cy + Math.sin(angle) * r, s.fill, s.alpha);
|
|
816
|
+
}
|
|
817
|
+
for (let i = 0;i < segments; i = i + 1) {
|
|
818
|
+
state.indices[state.index_count] = base_vertex;
|
|
819
|
+
state.indices[state.index_count + 1] = base_vertex + i + 1;
|
|
820
|
+
state.indices[state.index_count + 2] = base_vertex + i + 2;
|
|
821
|
+
state.index_count = state.index_count + 3;
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
if (s.stroke !== null && s.stroke_width > 0 && r > 0) {
|
|
825
|
+
const outer_r = r + s.stroke_width;
|
|
826
|
+
const base_vertex = state.vertex_count;
|
|
827
|
+
for (let i = 0;i <= segments; i = i + 1) {
|
|
828
|
+
const angle = i / segments * Math.PI * 2;
|
|
829
|
+
const cos = Math.cos(angle);
|
|
830
|
+
const sin = Math.sin(angle);
|
|
831
|
+
write_vertex_webgl(state, cx + cos * r, cy + sin * r, s.stroke, s.alpha);
|
|
832
|
+
write_vertex_webgl(state, cx + cos * outer_r, cy + sin * outer_r, s.stroke, s.alpha);
|
|
833
|
+
}
|
|
834
|
+
for (let i = 0;i < segments; i = i + 1) {
|
|
835
|
+
const base = base_vertex + i * 2;
|
|
836
|
+
state.indices[state.index_count] = base;
|
|
837
|
+
state.indices[state.index_count + 1] = base + 1;
|
|
838
|
+
state.indices[state.index_count + 2] = base + 2;
|
|
839
|
+
state.indices[state.index_count + 3] = base + 1;
|
|
840
|
+
state.indices[state.index_count + 4] = base + 3;
|
|
841
|
+
state.indices[state.index_count + 5] = base + 2;
|
|
842
|
+
state.index_count = state.index_count + 6;
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
function draw_line_webgl(state, a, b, s) {
|
|
847
|
+
if (s.stroke === null || s.stroke_width <= 0) {
|
|
848
|
+
return;
|
|
849
|
+
}
|
|
850
|
+
const dx = b[0] - a[0];
|
|
851
|
+
const dy = b[1] - a[1];
|
|
852
|
+
const len = Math.sqrt(dx * dx + dy * dy);
|
|
853
|
+
if (len === 0) {
|
|
854
|
+
return;
|
|
855
|
+
}
|
|
856
|
+
const width = s.stroke_width / 2;
|
|
857
|
+
const nx = -dy / len * width;
|
|
858
|
+
const ny = dx / len * width;
|
|
859
|
+
add_quad_webgl(state, a[0] + nx, a[1] + ny, b[0] + nx, b[1] + ny, b[0] - nx, b[1] - ny, a[0] - nx, a[1] - ny, s.stroke, s.alpha);
|
|
860
|
+
}
|
|
861
|
+
function draw_polyline_webgl(state, points, s) {
|
|
862
|
+
if (points.length < 2 || s.stroke === null || s.stroke_width <= 0) {
|
|
863
|
+
return;
|
|
864
|
+
}
|
|
865
|
+
for (let i = 0;i < points.length - 1; i = i + 1) {
|
|
866
|
+
const p1 = points[i];
|
|
867
|
+
const p2 = points[i + 1];
|
|
868
|
+
if (p1 !== undefined && p2 !== undefined) {
|
|
869
|
+
draw_line_webgl(state, p1, p2, s);
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
function add_quad_webgl(state, x1, y1, x2, y2, x3, y3, x4, y4, color, alpha) {
|
|
874
|
+
if (state.vertex_count + 4 > state.max_vertices || state.index_count + 6 > state.max_indices) {
|
|
875
|
+
flush_webgl(state);
|
|
876
|
+
}
|
|
877
|
+
const base = state.vertex_count;
|
|
878
|
+
write_vertex_webgl(state, x1, y1, color, alpha);
|
|
879
|
+
write_vertex_webgl(state, x2, y2, color, alpha);
|
|
880
|
+
write_vertex_webgl(state, x3, y3, color, alpha);
|
|
881
|
+
write_vertex_webgl(state, x4, y4, color, alpha);
|
|
882
|
+
state.indices[state.index_count] = base;
|
|
883
|
+
state.indices[state.index_count + 1] = base + 1;
|
|
884
|
+
state.indices[state.index_count + 2] = base + 2;
|
|
885
|
+
state.indices[state.index_count + 3] = base + 1;
|
|
886
|
+
state.indices[state.index_count + 4] = base + 3;
|
|
887
|
+
state.indices[state.index_count + 5] = base + 2;
|
|
888
|
+
state.index_count = state.index_count + 6;
|
|
889
|
+
}
|
|
890
|
+
function write_vertex_webgl(state, x, y, color, alpha) {
|
|
891
|
+
state.vertices[state.vertex_count * 2] = x;
|
|
892
|
+
state.vertices[state.vertex_count * 2 + 1] = y;
|
|
893
|
+
state.colors[state.vertex_count * 4] = color[0];
|
|
894
|
+
state.colors[state.vertex_count * 4 + 1] = color[1];
|
|
895
|
+
state.colors[state.vertex_count * 4 + 2] = color[2];
|
|
896
|
+
state.colors[state.vertex_count * 4 + 3] = color[3] * alpha;
|
|
897
|
+
state.vertex_count = state.vertex_count + 1;
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
// renderer/webgl/textures.ts
|
|
901
|
+
function draw_image_webgl(state, image, r, opacity, version) {
|
|
902
|
+
const texture = sync_texture_webgl(state, image, version);
|
|
903
|
+
if (texture === null) {
|
|
904
|
+
return;
|
|
905
|
+
}
|
|
906
|
+
if (state.index_count > 0) {
|
|
907
|
+
flush_webgl(state);
|
|
908
|
+
}
|
|
909
|
+
const gl = state.gl;
|
|
910
|
+
gl.useProgram(state.texture_program);
|
|
911
|
+
gl.uniformMatrix3fv(state.texture_locations.transform, false, state.current_transform);
|
|
912
|
+
gl.uniform2f(state.texture_locations.resolution, state.width, state.height);
|
|
913
|
+
gl.uniform1f(state.texture_locations.alpha, opacity);
|
|
914
|
+
const x = r[0];
|
|
915
|
+
const y = r[1];
|
|
916
|
+
const w = r[2];
|
|
917
|
+
const h = r[3];
|
|
918
|
+
const positions = new Float32Array([x, y, x + w, y, x, y + h, x, y + h, x + w, y, x + w, y + h]);
|
|
919
|
+
const texcoords = new Float32Array([0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1]);
|
|
920
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, state.position_buffer);
|
|
921
|
+
gl.bufferData(gl.ARRAY_BUFFER, positions, gl.DYNAMIC_DRAW);
|
|
922
|
+
gl.enableVertexAttribArray(state.texture_locations.position);
|
|
923
|
+
gl.vertexAttribPointer(state.texture_locations.position, 2, gl.FLOAT, false, 0, 0);
|
|
924
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, state.texcoord_buffer);
|
|
925
|
+
gl.bufferData(gl.ARRAY_BUFFER, texcoords, gl.DYNAMIC_DRAW);
|
|
926
|
+
gl.enableVertexAttribArray(state.texture_locations.texcoord);
|
|
927
|
+
gl.vertexAttribPointer(state.texture_locations.texcoord, 2, gl.FLOAT, false, 0, 0);
|
|
928
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
929
|
+
gl.bindTexture(gl.TEXTURE_2D, texture.texture);
|
|
930
|
+
gl.uniform1i(state.texture_locations.sampler, 0);
|
|
931
|
+
gl.drawArrays(gl.TRIANGLES, 0, 6);
|
|
932
|
+
}
|
|
933
|
+
function sync_texture_webgl(state, image, version) {
|
|
934
|
+
if (!is_image_ready_webgl(image)) {
|
|
935
|
+
return null;
|
|
936
|
+
}
|
|
937
|
+
const gl = state.gl;
|
|
938
|
+
const width = image instanceof HTMLImageElement ? image.naturalWidth : image.width;
|
|
939
|
+
const height = image instanceof HTMLImageElement ? image.naturalHeight : image.height;
|
|
940
|
+
const refresh_canvas_each_draw = image instanceof HTMLCanvasElement && version === undefined;
|
|
941
|
+
const source_key = image_source_key_webgl(image);
|
|
942
|
+
let texture = state.texture_cache.get(image);
|
|
943
|
+
if (texture === undefined) {
|
|
944
|
+
const created = gl.createTexture();
|
|
945
|
+
if (created === null) {
|
|
946
|
+
return null;
|
|
947
|
+
}
|
|
948
|
+
texture = {
|
|
949
|
+
texture: created,
|
|
950
|
+
width,
|
|
951
|
+
height,
|
|
952
|
+
version: version ?? null,
|
|
953
|
+
source_key
|
|
954
|
+
};
|
|
955
|
+
state.texture_cache.set(image, texture);
|
|
956
|
+
gl.bindTexture(gl.TEXTURE_2D, created);
|
|
957
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
958
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
959
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
|
960
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
961
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
|
|
962
|
+
return texture;
|
|
963
|
+
}
|
|
964
|
+
const size_changed = texture.width !== width || texture.height !== height;
|
|
965
|
+
const version_changed = version !== undefined && texture.version !== version;
|
|
966
|
+
const source_changed = texture.source_key !== source_key;
|
|
967
|
+
if (size_changed || refresh_canvas_each_draw || version_changed || source_changed) {
|
|
968
|
+
gl.bindTexture(gl.TEXTURE_2D, texture.texture);
|
|
969
|
+
if (size_changed) {
|
|
970
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
|
|
971
|
+
} else {
|
|
972
|
+
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, image);
|
|
973
|
+
}
|
|
974
|
+
texture.width = width;
|
|
975
|
+
texture.height = height;
|
|
976
|
+
texture.source_key = source_key;
|
|
977
|
+
}
|
|
978
|
+
if (version !== undefined) {
|
|
979
|
+
texture.version = version;
|
|
980
|
+
}
|
|
981
|
+
return texture;
|
|
982
|
+
}
|
|
983
|
+
function is_image_ready_webgl(image) {
|
|
984
|
+
if (image instanceof HTMLImageElement) {
|
|
985
|
+
return image.complete && image.naturalWidth > 0 && image.naturalHeight > 0;
|
|
986
|
+
}
|
|
987
|
+
return image.width > 0 && image.height > 0;
|
|
988
|
+
}
|
|
989
|
+
function image_source_key_webgl(image) {
|
|
990
|
+
if (image instanceof HTMLImageElement) {
|
|
991
|
+
return image.currentSrc || image.src || null;
|
|
992
|
+
}
|
|
993
|
+
if (typeof ImageBitmap !== "undefined" && image instanceof ImageBitmap) {
|
|
994
|
+
return `bitmap:${image.width}x${image.height}`;
|
|
995
|
+
}
|
|
996
|
+
return null;
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
// renderer/webgl/transforms.ts
|
|
1000
|
+
function set_transform_webgl(state, t) {
|
|
1001
|
+
state.current_transform[0] = t[0];
|
|
1002
|
+
state.current_transform[1] = t[2];
|
|
1003
|
+
state.current_transform[2] = t[4];
|
|
1004
|
+
state.current_transform[3] = t[1];
|
|
1005
|
+
state.current_transform[4] = t[3];
|
|
1006
|
+
state.current_transform[5] = t[5];
|
|
1007
|
+
state.current_transform[6] = 0;
|
|
1008
|
+
state.current_transform[7] = 0;
|
|
1009
|
+
state.current_transform[8] = 1;
|
|
1010
|
+
}
|
|
1011
|
+
function reset_transform_webgl(state) {
|
|
1012
|
+
state.current_transform[0] = 1;
|
|
1013
|
+
state.current_transform[1] = 0;
|
|
1014
|
+
state.current_transform[2] = 0;
|
|
1015
|
+
state.current_transform[3] = 0;
|
|
1016
|
+
state.current_transform[4] = 1;
|
|
1017
|
+
state.current_transform[5] = 0;
|
|
1018
|
+
state.current_transform[6] = 0;
|
|
1019
|
+
state.current_transform[7] = 0;
|
|
1020
|
+
state.current_transform[8] = 1;
|
|
1021
|
+
}
|
|
1022
|
+
function save_webgl(state) {
|
|
1023
|
+
const copy = new Float32Array(9);
|
|
1024
|
+
copy.set(state.current_transform);
|
|
1025
|
+
state.transform_stack.push(copy);
|
|
1026
|
+
}
|
|
1027
|
+
function restore_webgl(state) {
|
|
1028
|
+
const previous = state.transform_stack.pop();
|
|
1029
|
+
if (previous !== undefined) {
|
|
1030
|
+
state.current_transform.set(previous);
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
// renderer/canvas/paths.ts
|
|
1035
|
+
function get_or_create_path2d_canvas(state, p) {
|
|
1036
|
+
const hash = hash_of_path(p);
|
|
1037
|
+
const cached = state.path_cache.get(hash);
|
|
1038
|
+
if (cached !== undefined && paths_equal2(cached.path_commands, p.commands)) {
|
|
1039
|
+
state.cache_access_counter = state.cache_access_counter + 1;
|
|
1040
|
+
cached.last_used = state.cache_access_counter;
|
|
1041
|
+
return cached.path2d;
|
|
1042
|
+
}
|
|
1043
|
+
const path2d = build_path2d_canvas(p);
|
|
1044
|
+
if (state.path_cache.size >= state.cache_max_size) {
|
|
1045
|
+
evict_lru_canvas(state);
|
|
1046
|
+
}
|
|
1047
|
+
state.cache_access_counter = state.cache_access_counter + 1;
|
|
1048
|
+
state.path_cache.set(hash, {
|
|
1049
|
+
path2d,
|
|
1050
|
+
last_used: state.cache_access_counter,
|
|
1051
|
+
path_commands: p.commands
|
|
1052
|
+
});
|
|
1053
|
+
return path2d;
|
|
1054
|
+
}
|
|
1055
|
+
function paths_equal2(a, b) {
|
|
1056
|
+
if (a.length !== b.length) {
|
|
1057
|
+
return false;
|
|
1058
|
+
}
|
|
1059
|
+
for (let i = 0;i < a.length; i = i + 1) {
|
|
1060
|
+
const cmd_a = a[i];
|
|
1061
|
+
const cmd_b = b[i];
|
|
1062
|
+
if (cmd_a === undefined || cmd_b === undefined) {
|
|
1063
|
+
return false;
|
|
1064
|
+
}
|
|
1065
|
+
if (cmd_a.length !== cmd_b.length) {
|
|
1066
|
+
return false;
|
|
1067
|
+
}
|
|
1068
|
+
for (let j = 0;j < cmd_a.length; j = j + 1) {
|
|
1069
|
+
if (cmd_a[j] !== cmd_b[j]) {
|
|
1070
|
+
return false;
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
return true;
|
|
1075
|
+
}
|
|
1076
|
+
function build_path2d_canvas(p) {
|
|
1077
|
+
const path2d = new Path2D;
|
|
1078
|
+
for (let i = 0;i < p.commands.length; i = i + 1) {
|
|
1079
|
+
const cmd = p.commands[i];
|
|
1080
|
+
if (cmd === undefined) {
|
|
1081
|
+
continue;
|
|
1082
|
+
}
|
|
1083
|
+
const type = cmd[0];
|
|
1084
|
+
if (type === path_move) {
|
|
1085
|
+
path2d.moveTo(cmd[1], cmd[2]);
|
|
1086
|
+
} else if (type === path_line) {
|
|
1087
|
+
path2d.lineTo(cmd[1], cmd[2]);
|
|
1088
|
+
} else if (type === path_cubic) {
|
|
1089
|
+
path2d.bezierCurveTo(cmd[1], cmd[2], cmd[3], cmd[4], cmd[5], cmd[6]);
|
|
1090
|
+
} else if (type === path_close) {
|
|
1091
|
+
path2d.closePath();
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
return path2d;
|
|
1095
|
+
}
|
|
1096
|
+
function evict_lru_canvas(state) {
|
|
1097
|
+
let oldest_key = null;
|
|
1098
|
+
let oldest_access = Infinity;
|
|
1099
|
+
for (const [key, entry] of state.path_cache) {
|
|
1100
|
+
if (entry.last_used < oldest_access) {
|
|
1101
|
+
oldest_access = entry.last_used;
|
|
1102
|
+
oldest_key = key;
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
if (oldest_key !== null) {
|
|
1106
|
+
state.path_cache.delete(oldest_key);
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
// renderer/canvas/draw.ts
|
|
1111
|
+
function draw_path_canvas(state, p, s) {
|
|
1112
|
+
const path2d = get_or_create_path2d_canvas(state, p);
|
|
1113
|
+
apply_style_canvas(state.ctx, s);
|
|
1114
|
+
if (s.fill !== null) {
|
|
1115
|
+
state.ctx.fill(path2d);
|
|
1116
|
+
}
|
|
1117
|
+
if (s.stroke !== null && s.stroke_width > 0) {
|
|
1118
|
+
state.ctx.stroke(path2d);
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
function draw_rectangle_canvas(state, r, s) {
|
|
1122
|
+
apply_style_canvas(state.ctx, s);
|
|
1123
|
+
if (s.fill !== null) {
|
|
1124
|
+
state.ctx.fillRect(r[0], r[1], r[2], r[3]);
|
|
1125
|
+
}
|
|
1126
|
+
if (s.stroke !== null && s.stroke_width > 0) {
|
|
1127
|
+
state.ctx.strokeRect(r[0], r[1], r[2], r[3]);
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
function draw_circle_canvas(state, c, s) {
|
|
1131
|
+
state.ctx.beginPath();
|
|
1132
|
+
state.ctx.arc(c[0], c[1], c[2], 0, Math.PI * 2);
|
|
1133
|
+
apply_style_canvas(state.ctx, s);
|
|
1134
|
+
if (s.fill !== null) {
|
|
1135
|
+
state.ctx.fill();
|
|
1136
|
+
}
|
|
1137
|
+
if (s.stroke !== null && s.stroke_width > 0) {
|
|
1138
|
+
state.ctx.stroke();
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
function draw_line_canvas(state, a, b, s) {
|
|
1142
|
+
state.ctx.beginPath();
|
|
1143
|
+
state.ctx.moveTo(a[0], a[1]);
|
|
1144
|
+
state.ctx.lineTo(b[0], b[1]);
|
|
1145
|
+
apply_style_canvas(state.ctx, s);
|
|
1146
|
+
state.ctx.stroke();
|
|
1147
|
+
}
|
|
1148
|
+
function draw_polyline_canvas(state, points, s) {
|
|
1149
|
+
if (points.length < 2) {
|
|
1150
|
+
return;
|
|
1151
|
+
}
|
|
1152
|
+
state.ctx.beginPath();
|
|
1153
|
+
const first = points[0];
|
|
1154
|
+
if (first !== undefined) {
|
|
1155
|
+
state.ctx.moveTo(first[0], first[1]);
|
|
1156
|
+
}
|
|
1157
|
+
for (let i = 1;i < points.length; i = i + 1) {
|
|
1158
|
+
const point = points[i];
|
|
1159
|
+
if (point !== undefined) {
|
|
1160
|
+
state.ctx.lineTo(point[0], point[1]);
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
apply_style_canvas(state.ctx, s);
|
|
1164
|
+
state.ctx.stroke();
|
|
1165
|
+
}
|
|
1166
|
+
function draw_image_canvas(state, image, r, opacity, _version) {
|
|
1167
|
+
const old_alpha = state.ctx.globalAlpha;
|
|
1168
|
+
state.ctx.globalAlpha = opacity;
|
|
1169
|
+
state.ctx.drawImage(image, r[0], r[1], r[2], r[3]);
|
|
1170
|
+
state.ctx.globalAlpha = old_alpha;
|
|
1171
|
+
}
|
|
1172
|
+
function apply_style_canvas(ctx, s) {
|
|
1173
|
+
ctx.globalAlpha = s.alpha;
|
|
1174
|
+
if (s.fill !== null) {
|
|
1175
|
+
ctx.fillStyle = to_rgba_string_color(s.fill);
|
|
1176
|
+
}
|
|
1177
|
+
if (s.stroke !== null) {
|
|
1178
|
+
ctx.strokeStyle = to_rgba_string_color(s.stroke);
|
|
1179
|
+
}
|
|
1180
|
+
ctx.lineWidth = s.stroke_width;
|
|
1181
|
+
ctx.lineCap = line_cap_to_string_canvas(s.line_cap);
|
|
1182
|
+
ctx.lineJoin = line_join_to_string_canvas(s.line_join);
|
|
1183
|
+
ctx.miterLimit = s.miter_limit;
|
|
1184
|
+
}
|
|
1185
|
+
function line_cap_to_string_canvas(cap) {
|
|
1186
|
+
if (cap === line_cap_round) {
|
|
1187
|
+
return "round";
|
|
1188
|
+
} else if (cap === line_cap_square) {
|
|
1189
|
+
return "square";
|
|
1190
|
+
} else {
|
|
1191
|
+
return "butt";
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
function line_join_to_string_canvas(join) {
|
|
1195
|
+
if (join === line_join_round) {
|
|
1196
|
+
return "round";
|
|
1197
|
+
} else if (join === line_join_bevel) {
|
|
1198
|
+
return "bevel";
|
|
1199
|
+
} else {
|
|
1200
|
+
return "miter";
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
// renderer/canvas/lifecycle.ts
|
|
1205
|
+
function begin_frame_canvas(state) {
|
|
1206
|
+
state.ctx.setTransform(state.current_pixel_ratio, 0, 0, state.current_pixel_ratio, 0, 0);
|
|
1207
|
+
}
|
|
1208
|
+
function end_frame_canvas(_state) {}
|
|
1209
|
+
function set_transform_canvas(state, t) {
|
|
1210
|
+
const pr = state.current_pixel_ratio;
|
|
1211
|
+
state.ctx.setTransform(t[0] * pr, t[1] * pr, t[2] * pr, t[3] * pr, t[4] * pr, t[5] * pr);
|
|
1212
|
+
}
|
|
1213
|
+
function reset_transform_canvas(state) {
|
|
1214
|
+
state.ctx.setTransform(state.current_pixel_ratio, 0, 0, state.current_pixel_ratio, 0, 0);
|
|
1215
|
+
}
|
|
1216
|
+
function save_canvas(state) {
|
|
1217
|
+
state.ctx.save();
|
|
1218
|
+
}
|
|
1219
|
+
function restore_canvas(state) {
|
|
1220
|
+
state.ctx.restore();
|
|
1221
|
+
}
|
|
1222
|
+
function clip_rectangle_canvas(state, r) {
|
|
1223
|
+
state.ctx.save();
|
|
1224
|
+
state.ctx.beginPath();
|
|
1225
|
+
state.ctx.rect(r[0], r[1], r[2], r[3]);
|
|
1226
|
+
state.ctx.clip();
|
|
1227
|
+
}
|
|
1228
|
+
function clear_canvas(state, c) {
|
|
1229
|
+
state.ctx.save();
|
|
1230
|
+
state.ctx.resetTransform();
|
|
1231
|
+
state.ctx.fillStyle = to_rgba_string_color(c);
|
|
1232
|
+
state.ctx.fillRect(0, 0, state.current_width * state.current_pixel_ratio, state.current_height * state.current_pixel_ratio);
|
|
1233
|
+
state.ctx.restore();
|
|
1234
|
+
}
|
|
1235
|
+
function resize_canvas(state, canvas, width, height, pixel_ratio) {
|
|
1236
|
+
state.current_width = width;
|
|
1237
|
+
state.current_height = height;
|
|
1238
|
+
state.current_pixel_ratio = pixel_ratio;
|
|
1239
|
+
canvas.width = width * pixel_ratio;
|
|
1240
|
+
canvas.height = height * pixel_ratio;
|
|
1241
|
+
canvas.style.width = width + "px";
|
|
1242
|
+
canvas.style.height = height + "px";
|
|
1243
|
+
state.ctx.setTransform(pixel_ratio, 0, 0, pixel_ratio, 0, 0);
|
|
1244
|
+
}
|
|
1245
|
+
function dispose_canvas(state) {
|
|
1246
|
+
state.path_cache.clear();
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
// renderer/canvas/index.ts
|
|
1250
|
+
function create_renderer_canvas(canvas, config) {
|
|
1251
|
+
const ctx = canvas.getContext("2d");
|
|
1252
|
+
if (ctx === null) {
|
|
1253
|
+
return null;
|
|
1254
|
+
}
|
|
1255
|
+
const state = {
|
|
1256
|
+
ctx,
|
|
1257
|
+
path_cache: new Map,
|
|
1258
|
+
cache_access_counter: 0,
|
|
1259
|
+
cache_max_size: config.path_cache_size,
|
|
1260
|
+
current_width: canvas.width,
|
|
1261
|
+
current_height: canvas.height,
|
|
1262
|
+
current_pixel_ratio: 1
|
|
1263
|
+
};
|
|
1264
|
+
return {
|
|
1265
|
+
kind: renderer_kind_canvas,
|
|
1266
|
+
get width() {
|
|
1267
|
+
return state.current_width;
|
|
1268
|
+
},
|
|
1269
|
+
get height() {
|
|
1270
|
+
return state.current_height;
|
|
1271
|
+
},
|
|
1272
|
+
get pixel_ratio() {
|
|
1273
|
+
return state.current_pixel_ratio;
|
|
1274
|
+
},
|
|
1275
|
+
begin_frame: () => begin_frame_canvas(state),
|
|
1276
|
+
end_frame: () => end_frame_canvas(state),
|
|
1277
|
+
set_transform: (t) => set_transform_canvas(state, t),
|
|
1278
|
+
reset_transform: () => reset_transform_canvas(state),
|
|
1279
|
+
save: () => save_canvas(state),
|
|
1280
|
+
restore: () => restore_canvas(state),
|
|
1281
|
+
draw_path: (p, s) => draw_path_canvas(state, p, s),
|
|
1282
|
+
draw_rectangle: (r, s) => draw_rectangle_canvas(state, r, s),
|
|
1283
|
+
draw_circle: (c, s) => draw_circle_canvas(state, c, s),
|
|
1284
|
+
draw_line: (a, b, s) => draw_line_canvas(state, a, b, s),
|
|
1285
|
+
draw_polyline: (points, s) => draw_polyline_canvas(state, points, s),
|
|
1286
|
+
draw_image: (image, r, opacity, version) => draw_image_canvas(state, image, r, opacity, version),
|
|
1287
|
+
clip_rectangle: (r) => clip_rectangle_canvas(state, r),
|
|
1288
|
+
clear: (c) => clear_canvas(state, c),
|
|
1289
|
+
resize: (width, height, pixel_ratio) => resize_canvas(state, canvas, width, height, pixel_ratio),
|
|
1290
|
+
dispose: () => dispose_canvas(state)
|
|
1291
|
+
};
|
|
1292
|
+
}
|
|
1293
|
+
// renderer/renderer.ts
|
|
1294
|
+
var renderer_kind_canvas = 0;
|
|
1295
|
+
var renderer_kind_webgl = 1;
|
|
1296
|
+
function create_renderer_config() {
|
|
1297
|
+
return {
|
|
1298
|
+
prefer_webgl: false,
|
|
1299
|
+
path_cache_size: 1024
|
|
1300
|
+
};
|
|
1301
|
+
}
|
|
1302
|
+
function create_renderer(canvas, config) {
|
|
1303
|
+
if (config.prefer_webgl) {
|
|
1304
|
+
const webgl_renderer = create_renderer_webgl(canvas, config);
|
|
1305
|
+
if (webgl_renderer !== null) {
|
|
1306
|
+
return webgl_renderer;
|
|
1307
|
+
}
|
|
1308
|
+
}
|
|
1309
|
+
return create_renderer_canvas(canvas, config);
|
|
1310
|
+
}
|
|
1311
|
+
|
|
1312
|
+
// renderer/webgl/index.ts
|
|
1313
|
+
function create_renderer_webgl(canvas, config) {
|
|
1314
|
+
const gl = canvas.getContext("webgl", { alpha: true, antialias: true });
|
|
1315
|
+
if (gl === null) {
|
|
1316
|
+
return null;
|
|
1317
|
+
}
|
|
1318
|
+
const color_program = create_color_program_webgl(gl);
|
|
1319
|
+
const texture_program = create_texture_program_webgl(gl);
|
|
1320
|
+
if (color_program === null || texture_program === null) {
|
|
1321
|
+
if (color_program !== null) {
|
|
1322
|
+
gl.deleteProgram(color_program.program);
|
|
1323
|
+
}
|
|
1324
|
+
if (texture_program !== null) {
|
|
1325
|
+
gl.deleteProgram(texture_program.program);
|
|
1326
|
+
}
|
|
1327
|
+
return null;
|
|
1328
|
+
}
|
|
1329
|
+
const max_vertices = 65536;
|
|
1330
|
+
const max_indices = max_vertices * 3;
|
|
1331
|
+
const state = {
|
|
1332
|
+
gl,
|
|
1333
|
+
canvas,
|
|
1334
|
+
program: color_program.program,
|
|
1335
|
+
texture_program: texture_program.program,
|
|
1336
|
+
program_locations: color_program.locations,
|
|
1337
|
+
texture_locations: texture_program.locations,
|
|
1338
|
+
position_buffer: gl.createBuffer(),
|
|
1339
|
+
color_buffer: gl.createBuffer(),
|
|
1340
|
+
texcoord_buffer: gl.createBuffer(),
|
|
1341
|
+
index_buffer: gl.createBuffer(),
|
|
1342
|
+
vertices: new Float32Array(max_vertices * 2),
|
|
1343
|
+
colors: new Float32Array(max_vertices * 4),
|
|
1344
|
+
indices: new Uint16Array(max_indices),
|
|
1345
|
+
vertex_count: 0,
|
|
1346
|
+
index_count: 0,
|
|
1347
|
+
max_vertices,
|
|
1348
|
+
max_indices,
|
|
1349
|
+
current_transform: new Float32Array([1, 0, 0, 0, 1, 0, 0, 0, 1]),
|
|
1350
|
+
transform_stack: [],
|
|
1351
|
+
fill_cache: new Map,
|
|
1352
|
+
stroke_cache: new Map,
|
|
1353
|
+
texture_cache: new Map,
|
|
1354
|
+
cache_max_size: config.path_cache_size,
|
|
1355
|
+
width: canvas.width,
|
|
1356
|
+
height: canvas.height,
|
|
1357
|
+
pixel_ratio: 1,
|
|
1358
|
+
scissor_enabled: false
|
|
1359
|
+
};
|
|
1360
|
+
gl.enable(gl.BLEND);
|
|
1361
|
+
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
|
|
1362
|
+
return {
|
|
1363
|
+
kind: renderer_kind_webgl,
|
|
1364
|
+
get width() {
|
|
1365
|
+
return state.width;
|
|
1366
|
+
},
|
|
1367
|
+
get height() {
|
|
1368
|
+
return state.height;
|
|
1369
|
+
},
|
|
1370
|
+
get pixel_ratio() {
|
|
1371
|
+
return state.pixel_ratio;
|
|
1372
|
+
},
|
|
1373
|
+
begin_frame: () => begin_frame_webgl(state),
|
|
1374
|
+
end_frame: () => end_frame_webgl(state),
|
|
1375
|
+
set_transform: (t) => set_transform_webgl(state, t),
|
|
1376
|
+
reset_transform: () => reset_transform_webgl(state),
|
|
1377
|
+
save: () => save_webgl(state),
|
|
1378
|
+
restore: () => restore_webgl(state),
|
|
1379
|
+
draw_path: (p, s) => draw_path_webgl(state, p, s),
|
|
1380
|
+
draw_rectangle: (r, s) => draw_rectangle_webgl(state, r, s),
|
|
1381
|
+
draw_circle: (c, s) => draw_circle_webgl(state, c, s),
|
|
1382
|
+
draw_line: (a, b, s) => draw_line_webgl(state, a, b, s),
|
|
1383
|
+
draw_polyline: (points, s) => draw_polyline_webgl(state, points, s),
|
|
1384
|
+
draw_image: (image, r, opacity, version) => draw_image_webgl(state, image, r, opacity, version),
|
|
1385
|
+
clip_rectangle: (r) => clip_rectangle_webgl(state, r),
|
|
1386
|
+
clear: (c) => clear_webgl(state, c),
|
|
1387
|
+
resize: (width, height, pixel_ratio) => resize_webgl(state, width, height, pixel_ratio),
|
|
1388
|
+
dispose: () => dispose_webgl(state)
|
|
1389
|
+
};
|
|
1390
|
+
}
|
|
1391
|
+
|
|
1392
|
+
export { path_move, path_line, path_cubic, path_close, create_path, clone_path, move_to_path, line_to_path, cubic_to_path, close_path, bounds_of_path, is_empty_path, command_count_of_path, transform_path, hash_of_path, from_rectangle_path, from_circle_path, from_polyline_path, create_renderer_webgl, renderer_kind_canvas, renderer_kind_webgl, create_renderer_config, create_renderer, create_renderer_canvas };
|