@g9tm/lynxcontrols-canvas 0.0.0-stage → 2.5.9
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/README.md +39 -2
- package/catalog/cards/G9Canvas.md +17 -0
- package/catalog/cards/G9CanvasStylePanel.md +13 -0
- package/catalog/catalog.json +2334 -0
- package/catalog/index.md +6 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/board-controller.d.ts +161 -0
- package/dist/board-controller.d.ts.map +1 -0
- package/dist/board-controller.jsx +812 -0
- package/dist/board-model.d.ts +42 -0
- package/dist/board-model.d.ts.map +1 -0
- package/dist/board-model.jsx +66 -0
- package/dist/canvas.css +81 -0
- package/dist/canvas.css.map +1 -0
- package/dist/canvas.d.ts +144 -0
- package/dist/canvas.d.ts.map +1 -0
- package/dist/canvas.jsx +481 -0
- package/dist/element-core.d.ts +155 -0
- package/dist/element-core.d.ts.map +1 -0
- package/dist/element-core.jsx +1104 -0
- package/dist/export.d.ts +7 -0
- package/dist/export.d.ts.map +1 -0
- package/dist/export.jsx +75 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.jsx +7 -0
- package/dist/observable.d.ts +5 -0
- package/dist/observable.d.ts.map +1 -0
- package/dist/observable.jsx +14 -0
- package/dist/protocol.d.ts +258 -0
- package/dist/protocol.d.ts.map +1 -0
- package/dist/protocol.jsx +2 -0
- package/dist/scene/edit.d.ts +59 -0
- package/dist/scene/edit.d.ts.map +1 -0
- package/dist/scene/edit.jsx +895 -0
- package/dist/scene/elements.d.ts +160 -0
- package/dist/scene/elements.d.ts.map +1 -0
- package/dist/scene/elements.jsx +392 -0
- package/dist/scene/geometry.d.ts +36 -0
- package/dist/scene/geometry.d.ts.map +1 -0
- package/dist/scene/geometry.jsx +367 -0
- package/dist/scene/index.d.ts +8 -0
- package/dist/scene/index.d.ts.map +1 -0
- package/dist/scene/index.jsx +6 -0
- package/dist/scene/path-data-parser.d.ts +17 -0
- package/dist/scene/path-data-parser.d.ts.map +1 -0
- package/dist/scene/path-data-parser.jsx +128 -0
- package/dist/scene/scene.d.ts +58 -0
- package/dist/scene/scene.d.ts.map +1 -0
- package/dist/scene/scene.jsx +169 -0
- package/dist/scene/shapes.d.ts +24 -0
- package/dist/scene/shapes.d.ts.map +1 -0
- package/dist/scene/shapes.jsx +807 -0
- package/dist/scene/types.d.ts +118 -0
- package/dist/scene/types.d.ts.map +1 -0
- package/dist/scene/types.jsx +1 -0
- package/dist/words.d.ts +100 -0
- package/dist/words.d.ts.map +1 -0
- package/dist/words.jsx +186 -0
- package/package.json +50 -4
package/dist/canvas.jsx
ADDED
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
import { g9IdProps } from "@g9tm/lynxcontrols/base";
|
|
2
|
+
import { useEffect, useMemo, useRef, useState, useSyncExternalStore } from "@lynx-js/react";
|
|
3
|
+
import { BACKGROUND_COLORS, FONT_FAMILY, STROKE_COLORS } from "./scene/elements.jsx";
|
|
4
|
+
import { g9CanvasWordsEn } from "./words.jsx";
|
|
5
|
+
import "./canvas.css";
|
|
6
|
+
function _define_property(obj, key, value) {
|
|
7
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
8
|
+
value: value,
|
|
9
|
+
enumerable: true,
|
|
10
|
+
configurable: true,
|
|
11
|
+
writable: true
|
|
12
|
+
});
|
|
13
|
+
else obj[key] = value;
|
|
14
|
+
return obj;
|
|
15
|
+
}
|
|
16
|
+
class G9CanvasPort {
|
|
17
|
+
attach(target) {
|
|
18
|
+
this.target = target;
|
|
19
|
+
}
|
|
20
|
+
attachDesktop(desktop) {
|
|
21
|
+
this.desktop = desktop;
|
|
22
|
+
}
|
|
23
|
+
get attached() {
|
|
24
|
+
return null !== this.target || null !== this.desktop;
|
|
25
|
+
}
|
|
26
|
+
exportImage(options) {
|
|
27
|
+
const target = this.target;
|
|
28
|
+
const desktop = this.desktop;
|
|
29
|
+
if (!target && !desktop) return Promise.reject(new Error('no_canvas'));
|
|
30
|
+
const token = Math.random().toString(36).slice(2);
|
|
31
|
+
return new Promise((resolve, reject)=>{
|
|
32
|
+
const timer = setTimeout(()=>{
|
|
33
|
+
this.exports.delete(token);
|
|
34
|
+
reject(new Error('export_timeout'));
|
|
35
|
+
}, 30000);
|
|
36
|
+
this.exports.set(token, (event)=>{
|
|
37
|
+
clearTimeout(timer);
|
|
38
|
+
resolve(event);
|
|
39
|
+
});
|
|
40
|
+
if (desktop) desktop.host.send(desktop.id, {
|
|
41
|
+
type: 'export',
|
|
42
|
+
params: {
|
|
43
|
+
token,
|
|
44
|
+
...options
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
else null == target || target.invoke({
|
|
48
|
+
method: 'exportImage',
|
|
49
|
+
params: {
|
|
50
|
+
token,
|
|
51
|
+
...options
|
|
52
|
+
}
|
|
53
|
+
}).exec();
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
exported(event) {
|
|
57
|
+
const done = this.exports.get(event.token);
|
|
58
|
+
this.exports.delete(event.token);
|
|
59
|
+
null == done || done(event);
|
|
60
|
+
}
|
|
61
|
+
constructor(){
|
|
62
|
+
_define_property(this, "target", null);
|
|
63
|
+
_define_property(this, "desktop", null);
|
|
64
|
+
_define_property(this, "exports", new Map());
|
|
65
|
+
_define_property(this, "send", (message)=>{
|
|
66
|
+
var _this_target;
|
|
67
|
+
if (this.desktop) return void this.desktop.host.send(this.desktop.id, {
|
|
68
|
+
type: 'apply',
|
|
69
|
+
json: JSON.stringify(message)
|
|
70
|
+
});
|
|
71
|
+
null == (_this_target = this.target) || _this_target.invoke({
|
|
72
|
+
method: 'apply',
|
|
73
|
+
params: {
|
|
74
|
+
json: JSON.stringify(message)
|
|
75
|
+
}
|
|
76
|
+
}).exec();
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function useG9CanvasState(controller) {
|
|
81
|
+
const version = useRef(0);
|
|
82
|
+
useSyncExternalStore((notify)=>controller.subscribe(()=>{
|
|
83
|
+
version.current++;
|
|
84
|
+
notify();
|
|
85
|
+
}), ()=>version.current);
|
|
86
|
+
return controller.state;
|
|
87
|
+
}
|
|
88
|
+
function dispatchG9CanvasEvent(controller, port, name, detail) {
|
|
89
|
+
const d = detail;
|
|
90
|
+
switch(name){
|
|
91
|
+
case 'cvview':
|
|
92
|
+
controller.onView(d);
|
|
93
|
+
return;
|
|
94
|
+
case 'cvdown':
|
|
95
|
+
controller.onDown(d);
|
|
96
|
+
return;
|
|
97
|
+
case 'cvtap':
|
|
98
|
+
controller.onTap(d);
|
|
99
|
+
return;
|
|
100
|
+
case 'cvmove':
|
|
101
|
+
controller.onMove(d);
|
|
102
|
+
return;
|
|
103
|
+
case 'cvtransform':
|
|
104
|
+
controller.onTransform(d);
|
|
105
|
+
return;
|
|
106
|
+
case 'cvbox':
|
|
107
|
+
controller.onBox(d);
|
|
108
|
+
return;
|
|
109
|
+
case 'cvshape':
|
|
110
|
+
controller.onShape(d);
|
|
111
|
+
return;
|
|
112
|
+
case 'cvline':
|
|
113
|
+
controller.onLine(d);
|
|
114
|
+
return;
|
|
115
|
+
case 'cvpoint':
|
|
116
|
+
controller.onPoint(d);
|
|
117
|
+
return;
|
|
118
|
+
case 'cvstroke':
|
|
119
|
+
controller.onStroke(d);
|
|
120
|
+
return;
|
|
121
|
+
case 'cverase':
|
|
122
|
+
controller.onErase(d);
|
|
123
|
+
return;
|
|
124
|
+
case 'cvcursor':
|
|
125
|
+
{
|
|
126
|
+
const c = detail;
|
|
127
|
+
controller.onCursor(c.x, c.y, c.down, Boolean(c.gone));
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
case 'cvexport':
|
|
131
|
+
port.exported(detail);
|
|
132
|
+
return;
|
|
133
|
+
default:
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const EVENTS = [
|
|
137
|
+
'cvview',
|
|
138
|
+
'cvdown',
|
|
139
|
+
'cvtap',
|
|
140
|
+
'cvmove',
|
|
141
|
+
'cvtransform',
|
|
142
|
+
'cvbox',
|
|
143
|
+
'cvshape',
|
|
144
|
+
'cvline',
|
|
145
|
+
'cvpoint',
|
|
146
|
+
'cvstroke',
|
|
147
|
+
'cverase',
|
|
148
|
+
'cvcursor',
|
|
149
|
+
'cvexport'
|
|
150
|
+
];
|
|
151
|
+
function G9Canvas(props) {
|
|
152
|
+
var _props_background, _props_accent;
|
|
153
|
+
if (props.desktop) return <DesktopCanvas {...props} desktop={props.desktop}/>;
|
|
154
|
+
const { controller, port } = props;
|
|
155
|
+
const ref = useRef(null);
|
|
156
|
+
useEffect(()=>{
|
|
157
|
+
port.attach(ref.current);
|
|
158
|
+
controller.attach();
|
|
159
|
+
return ()=>{
|
|
160
|
+
controller.detach();
|
|
161
|
+
port.attach(null);
|
|
162
|
+
};
|
|
163
|
+
}, [
|
|
164
|
+
controller,
|
|
165
|
+
port
|
|
166
|
+
]);
|
|
167
|
+
const handlers = {};
|
|
168
|
+
for (const name of EVENTS)handlers[`bind${name}`] = (e)=>dispatchG9CanvasEvent(controller, port, name, e.detail);
|
|
169
|
+
return <g9-canvas ref={ref} className={`g9-canvas${props.className ? ` ${props.className}` : ''}`} {...g9IdProps(props.id)} tool={props.tool} editable={props.editable} theme={props.dark ? 'dark' : 'light'} background={null != (_props_background = props.background) ? _props_background : '#ffffff'} accent={null != (_props_accent = props.accent) ? _props_accent : '#6965db'} stroke={JSON.stringify(props.stroke)} {...handlers}/>;
|
|
170
|
+
}
|
|
171
|
+
let canvasSeq = 0;
|
|
172
|
+
function DesktopCanvas(props) {
|
|
173
|
+
var _props_background, _props_accent;
|
|
174
|
+
const { controller, port, desktop } = props;
|
|
175
|
+
const id = useMemo(()=>`g9cv${++canvasSeq}`, [
|
|
176
|
+
controller
|
|
177
|
+
]);
|
|
178
|
+
const [ready, setReady] = useState(false);
|
|
179
|
+
const attrs = {
|
|
180
|
+
tool: props.tool,
|
|
181
|
+
editable: props.editable ? 'true' : 'false',
|
|
182
|
+
theme: props.dark ? 'dark' : 'light',
|
|
183
|
+
background: null != (_props_background = props.background) ? _props_background : '#ffffff',
|
|
184
|
+
accent: null != (_props_accent = props.accent) ? _props_accent : '#6965db',
|
|
185
|
+
stroke: JSON.stringify(props.stroke)
|
|
186
|
+
};
|
|
187
|
+
const attrsRef = useRef(attrs);
|
|
188
|
+
attrsRef.current = attrs;
|
|
189
|
+
useEffect(()=>{
|
|
190
|
+
let live = true;
|
|
191
|
+
const off = desktop.onEvent((event)=>{
|
|
192
|
+
if (event.id !== id) return;
|
|
193
|
+
if ('cvready' === event.name) {
|
|
194
|
+
port.attachDesktop({
|
|
195
|
+
id,
|
|
196
|
+
host: desktop
|
|
197
|
+
});
|
|
198
|
+
desktop.send(id, {
|
|
199
|
+
type: 'attrs',
|
|
200
|
+
attrs: attrsRef.current
|
|
201
|
+
});
|
|
202
|
+
controller.attach();
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
dispatchG9CanvasEvent(controller, port, event.name, event.detail);
|
|
206
|
+
});
|
|
207
|
+
desktop.prepare().then((ok)=>{
|
|
208
|
+
if (live) setReady(ok);
|
|
209
|
+
});
|
|
210
|
+
return ()=>{
|
|
211
|
+
live = false;
|
|
212
|
+
off();
|
|
213
|
+
controller.detach();
|
|
214
|
+
port.attachDesktop(null);
|
|
215
|
+
};
|
|
216
|
+
}, [
|
|
217
|
+
controller,
|
|
218
|
+
port,
|
|
219
|
+
id
|
|
220
|
+
]);
|
|
221
|
+
const key = JSON.stringify(attrs);
|
|
222
|
+
useEffect(()=>{
|
|
223
|
+
if (port.attached) desktop.send(id, {
|
|
224
|
+
type: 'attrs',
|
|
225
|
+
attrs
|
|
226
|
+
});
|
|
227
|
+
}, [
|
|
228
|
+
key
|
|
229
|
+
]);
|
|
230
|
+
const url = desktop.pageUrl(id);
|
|
231
|
+
const cls = `g9-canvas${props.className ? ` ${props.className}` : ''}`;
|
|
232
|
+
if (!url || !ready) return <view className={cls} {...g9IdProps(props.id)}/>;
|
|
233
|
+
return <webview src={url} use-osr={true} native-interaction-enabled={true} className={cls} {...g9IdProps(props.id)}/>;
|
|
234
|
+
}
|
|
235
|
+
function Section(props) {
|
|
236
|
+
return <view className="g9-cvpanel__section">
|
|
237
|
+
<text className="g9-cvpanel__label">{props.label}</text>
|
|
238
|
+
<view className="g9-cvpanel__row">{props.children}</view>
|
|
239
|
+
</view>;
|
|
240
|
+
}
|
|
241
|
+
function Swatch(props) {
|
|
242
|
+
const none = 'transparent' === props.color;
|
|
243
|
+
return <view className={props.active ? 'g9-cvpanel__swatch g9-cvpanel__swatch--on' : 'g9-cvpanel__swatch'} {...g9IdProps(props.id)} style={{
|
|
244
|
+
backgroundColor: none ? '#ffffff' : props.color
|
|
245
|
+
}} bindtap={props.onTap} accessibility-element={true} accessibility-traits="button" accessibility-label={props.label} {...props.active ? {
|
|
246
|
+
'accessibility-value': '✓'
|
|
247
|
+
} : {}}>
|
|
248
|
+
{none ? <text className="g9-cvpanel__none">⧄</text> : null}
|
|
249
|
+
</view>;
|
|
250
|
+
}
|
|
251
|
+
function Choice(props) {
|
|
252
|
+
var _props_glyph;
|
|
253
|
+
return <view className={props.active ? 'g9-cvpanel__choice g9-cvpanel__choice--on' : 'g9-cvpanel__choice'} {...g9IdProps(props.id)} bindtap={props.onTap} accessibility-element={true} accessibility-traits="button" accessibility-label={props.label} {...props.active ? {
|
|
254
|
+
'accessibility-value': '✓'
|
|
255
|
+
} : {}}>
|
|
256
|
+
<text className={props.active ? 'g9-cvpanel__choice-text g9-cvpanel__choice-text--on' : 'g9-cvpanel__choice-text'}>
|
|
257
|
+
{null != (_props_glyph = props.glyph) ? _props_glyph : props.label}
|
|
258
|
+
</text>
|
|
259
|
+
</view>;
|
|
260
|
+
}
|
|
261
|
+
function G9CanvasStylePanel(props) {
|
|
262
|
+
var _props_words;
|
|
263
|
+
const s = (null != (_props_words = props.words) ? _props_words : g9CanvasWordsEn).style;
|
|
264
|
+
const types = new Set(props.selected.length ? props.selected.map((e)=>e.type) : [
|
|
265
|
+
props.tool
|
|
266
|
+
]);
|
|
267
|
+
const has = (...list)=>list.some((t)=>types.has(t));
|
|
268
|
+
const shapes = has('rectangle', 'ellipse', 'diamond', 'line', 'freedraw');
|
|
269
|
+
const strokes = has('rectangle', 'ellipse', 'diamond', 'arrow', 'line', 'freedraw');
|
|
270
|
+
const lines = has('rectangle', 'ellipse', 'diamond', 'arrow', 'line');
|
|
271
|
+
const edges = has('rectangle', 'diamond', 'line', 'arrow', 'image');
|
|
272
|
+
const texts = has('text') || props.selected.some((e)=>{
|
|
273
|
+
var _e_boundElements;
|
|
274
|
+
return null == (_e_boundElements = e.boundElements) ? void 0 : _e_boundElements.some((b)=>'text' === b.type);
|
|
275
|
+
});
|
|
276
|
+
const arrows = has('arrow');
|
|
277
|
+
const style = props.style;
|
|
278
|
+
const set = (patch)=>props.controller.setStyle(patch);
|
|
279
|
+
const strokeColor = !(has('image') && 1 === types.size);
|
|
280
|
+
const id = (k)=>props.id ? `${props.id}_${k}` : void 0;
|
|
281
|
+
const choices = (key, list)=>list.map(([value, label, glyph])=><Choice key={String(value)} id={id(`${String(key)}_${String(value)}`)} label={label} {...glyph ? {
|
|
282
|
+
glyph
|
|
283
|
+
} : {}} active={style[key] === value} onTap={()=>set({
|
|
284
|
+
[key]: value
|
|
285
|
+
})}/>);
|
|
286
|
+
return <view className="g9-cvpanel" {...g9IdProps(props.id)}>
|
|
287
|
+
{strokeColor ? <Section label={s.stroke}>
|
|
288
|
+
{STROKE_COLORS.map((c)=><Swatch key={c} id={id(`stroke_${c}`)} color={c} label={c} active={style.strokeColor === c} onTap={()=>set({
|
|
289
|
+
strokeColor: c
|
|
290
|
+
})}/>)}
|
|
291
|
+
</Section> : null}
|
|
292
|
+
{shapes ? <Section label={s.background}>
|
|
293
|
+
{BACKGROUND_COLORS.map((c)=><Swatch key={c} id={id(`background_${c}`)} color={c} label={'transparent' === c ? s.transparent : c} active={style.backgroundColor === c} onTap={()=>set({
|
|
294
|
+
backgroundColor: c
|
|
295
|
+
})}/>)}
|
|
296
|
+
</Section> : null}
|
|
297
|
+
{shapes && 'transparent' !== style.backgroundColor ? <Section label={s.fill}>
|
|
298
|
+
{choices('fillStyle', [
|
|
299
|
+
[
|
|
300
|
+
'hachure',
|
|
301
|
+
s.hachure
|
|
302
|
+
],
|
|
303
|
+
[
|
|
304
|
+
'cross-hatch',
|
|
305
|
+
s.crossHatch
|
|
306
|
+
],
|
|
307
|
+
[
|
|
308
|
+
'solid',
|
|
309
|
+
s.solid
|
|
310
|
+
]
|
|
311
|
+
])}
|
|
312
|
+
</Section> : null}
|
|
313
|
+
{strokes ? <Section label={s.strokeWidth}>
|
|
314
|
+
{choices('strokeWidth', [
|
|
315
|
+
[
|
|
316
|
+
1,
|
|
317
|
+
s.thin
|
|
318
|
+
],
|
|
319
|
+
[
|
|
320
|
+
2,
|
|
321
|
+
s.bold
|
|
322
|
+
],
|
|
323
|
+
[
|
|
324
|
+
4,
|
|
325
|
+
s.extraBold
|
|
326
|
+
]
|
|
327
|
+
])}
|
|
328
|
+
</Section> : null}
|
|
329
|
+
{lines ? <>
|
|
330
|
+
<Section label={s.strokeStyle}>
|
|
331
|
+
{choices('strokeStyle', [
|
|
332
|
+
[
|
|
333
|
+
'solid',
|
|
334
|
+
s.solid
|
|
335
|
+
],
|
|
336
|
+
[
|
|
337
|
+
'dashed',
|
|
338
|
+
s.dashed
|
|
339
|
+
],
|
|
340
|
+
[
|
|
341
|
+
'dotted',
|
|
342
|
+
s.dotted
|
|
343
|
+
]
|
|
344
|
+
])}
|
|
345
|
+
</Section>
|
|
346
|
+
<Section label={s.sloppiness}>
|
|
347
|
+
{choices('roughness', [
|
|
348
|
+
[
|
|
349
|
+
0,
|
|
350
|
+
s.architect
|
|
351
|
+
],
|
|
352
|
+
[
|
|
353
|
+
1,
|
|
354
|
+
s.artist
|
|
355
|
+
],
|
|
356
|
+
[
|
|
357
|
+
2,
|
|
358
|
+
s.cartoonist
|
|
359
|
+
]
|
|
360
|
+
])}
|
|
361
|
+
</Section>
|
|
362
|
+
</> : null}
|
|
363
|
+
{edges ? <Section label={s.edges}>
|
|
364
|
+
{choices('roundness', [
|
|
365
|
+
[
|
|
366
|
+
'sharp',
|
|
367
|
+
s.sharp
|
|
368
|
+
],
|
|
369
|
+
[
|
|
370
|
+
'round',
|
|
371
|
+
s.round
|
|
372
|
+
]
|
|
373
|
+
])}
|
|
374
|
+
</Section> : null}
|
|
375
|
+
{arrows ? <Section label={s.arrowheads}>
|
|
376
|
+
{choices('endArrowhead', [
|
|
377
|
+
[
|
|
378
|
+
null,
|
|
379
|
+
`${s.end}: ${s.none}`
|
|
380
|
+
],
|
|
381
|
+
[
|
|
382
|
+
'arrow',
|
|
383
|
+
`${s.end}: →`
|
|
384
|
+
],
|
|
385
|
+
[
|
|
386
|
+
'triangle',
|
|
387
|
+
`${s.end}: ▶`
|
|
388
|
+
],
|
|
389
|
+
[
|
|
390
|
+
'dot',
|
|
391
|
+
`${s.end}: ●`
|
|
392
|
+
],
|
|
393
|
+
[
|
|
394
|
+
'bar',
|
|
395
|
+
`${s.end}: |`
|
|
396
|
+
]
|
|
397
|
+
])}
|
|
398
|
+
{choices('startArrowhead', [
|
|
399
|
+
[
|
|
400
|
+
null,
|
|
401
|
+
`${s.start}: ${s.none}`
|
|
402
|
+
],
|
|
403
|
+
[
|
|
404
|
+
'arrow',
|
|
405
|
+
`${s.start}: ←`
|
|
406
|
+
]
|
|
407
|
+
])}
|
|
408
|
+
</Section> : null}
|
|
409
|
+
{texts || 'text' === props.tool ? <>
|
|
410
|
+
<Section label={s.fontSize}>
|
|
411
|
+
{choices('fontSize', [
|
|
412
|
+
[
|
|
413
|
+
16,
|
|
414
|
+
s.small
|
|
415
|
+
],
|
|
416
|
+
[
|
|
417
|
+
20,
|
|
418
|
+
s.medium
|
|
419
|
+
],
|
|
420
|
+
[
|
|
421
|
+
28,
|
|
422
|
+
s.large
|
|
423
|
+
],
|
|
424
|
+
[
|
|
425
|
+
36,
|
|
426
|
+
s.huge
|
|
427
|
+
]
|
|
428
|
+
])}
|
|
429
|
+
</Section>
|
|
430
|
+
<Section label={s.fontFamily}>
|
|
431
|
+
{choices('fontFamily', [
|
|
432
|
+
[
|
|
433
|
+
FONT_FAMILY.Excalifont,
|
|
434
|
+
s.handDrawn
|
|
435
|
+
],
|
|
436
|
+
[
|
|
437
|
+
FONT_FAMILY.Nunito,
|
|
438
|
+
s.normal
|
|
439
|
+
],
|
|
440
|
+
[
|
|
441
|
+
FONT_FAMILY["Comic Shanns"],
|
|
442
|
+
s.code
|
|
443
|
+
]
|
|
444
|
+
])}
|
|
445
|
+
</Section>
|
|
446
|
+
<Section label={s.textAlign}>
|
|
447
|
+
{choices('textAlign', [
|
|
448
|
+
[
|
|
449
|
+
'left',
|
|
450
|
+
s.left
|
|
451
|
+
],
|
|
452
|
+
[
|
|
453
|
+
'center',
|
|
454
|
+
s.center
|
|
455
|
+
],
|
|
456
|
+
[
|
|
457
|
+
'right',
|
|
458
|
+
s.right
|
|
459
|
+
]
|
|
460
|
+
])}
|
|
461
|
+
</Section>
|
|
462
|
+
</> : null}
|
|
463
|
+
<Section label={s.opacity}>
|
|
464
|
+
{choices('opacity', [
|
|
465
|
+
30,
|
|
466
|
+
60,
|
|
467
|
+
100
|
|
468
|
+
].map((v)=>[
|
|
469
|
+
v,
|
|
470
|
+
`${v}%`
|
|
471
|
+
]))}
|
|
472
|
+
</Section>
|
|
473
|
+
{props.selected.length ? <Section label={s.layers}>
|
|
474
|
+
<Choice id={id('back')} label={s.toBack} glyph="⤓" active={false} onTap={()=>props.controller.reorder('back')}/>
|
|
475
|
+
<Choice id={id('backward')} label={s.backward} glyph="↓" active={false} onTap={()=>props.controller.reorder('backward')}/>
|
|
476
|
+
<Choice id={id('forward')} label={s.forward} glyph="↑" active={false} onTap={()=>props.controller.reorder('forward')}/>
|
|
477
|
+
<Choice id={id('front')} label={s.toFront} glyph="⤒" active={false} onTap={()=>props.controller.reorder('front')}/>
|
|
478
|
+
</Section> : null}
|
|
479
|
+
</view>;
|
|
480
|
+
}
|
|
481
|
+
export { G9Canvas, G9CanvasPort, G9CanvasStylePanel, dispatchG9CanvasEvent, useG9CanvasState };
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import type { CvBox, CvHandle, CvItem, CvMessage, CvPeer, CvSelection, CvStrokeStyle, CvTool, CvView } from './protocol.js';
|
|
2
|
+
/**
|
|
3
|
+
* The element side of `<g9-canvas>` without any drawing API: the items the app sent, the camera, and the gesture rules
|
|
4
|
+
* (what a finger or mouse does with each tool, previews, when to report what). The web host's element
|
|
5
|
+
* (client/web `src/canvas.ts`) runs this class and paints its state with Canvas 2D; the Android element
|
|
6
|
+
* (`whiteboard/G9CanvasCore.kt`) is a line-by-line Kotlin copy. Tests drive it with synthetic pointers.
|
|
7
|
+
*
|
|
8
|
+
* Rendering contract for hosts: the *scene layer* (background + every item in `order`, minus `hidden`, minus the
|
|
9
|
+
* moving set while a transform is live, `faded` at 30 %) is cached and re-rendered only when `sceneVersion` changes or
|
|
10
|
+
* the camera settled somewhere else; while panning/pinching the cache is drawn scaled/translated. The *overlay* (moving
|
|
11
|
+
* items through `liveMatrix()`, previews, selection, marquee, eraser trail, collaborators) is drawn every frame.
|
|
12
|
+
*/
|
|
13
|
+
export interface PointerInput {
|
|
14
|
+
id: number;
|
|
15
|
+
/** Element px from the top-left. */
|
|
16
|
+
x: number;
|
|
17
|
+
y: number;
|
|
18
|
+
type: 'touch' | 'mouse' | 'pen';
|
|
19
|
+
pressure?: number;
|
|
20
|
+
button?: number;
|
|
21
|
+
shift?: boolean;
|
|
22
|
+
alt?: boolean;
|
|
23
|
+
ctrl?: boolean;
|
|
24
|
+
}
|
|
25
|
+
/** What the element's core needs of its host: emit an event, ask for a frame, the clock. */
|
|
26
|
+
export interface CoreHost {
|
|
27
|
+
emit(name: string, detail: unknown): void;
|
|
28
|
+
/** Draw again (overlay; the scene layer when `sceneVersion` moved). */
|
|
29
|
+
invalidate(): void;
|
|
30
|
+
now(): number;
|
|
31
|
+
}
|
|
32
|
+
/** 2D affine matrix [a, b, c, d, e, f] (x' = a·x + c·y + e, y' = b·x + d·y + f), in scene units. */
|
|
33
|
+
export type Matrix = [number, number, number, number, number, number];
|
|
34
|
+
/** What the overlay draws for a gesture in progress (a stroke, a shape, a line, a point being moved). */
|
|
35
|
+
export type Preview = {
|
|
36
|
+
kind: 'shape';
|
|
37
|
+
tool: 'rectangle' | 'ellipse' | 'diamond' | 'frame';
|
|
38
|
+
b: CvBox;
|
|
39
|
+
} | {
|
|
40
|
+
kind: 'line';
|
|
41
|
+
tool: 'arrow' | 'line';
|
|
42
|
+
points: [number, number][];
|
|
43
|
+
} | {
|
|
44
|
+
kind: 'stroke';
|
|
45
|
+
points: number[];
|
|
46
|
+
pressures: number[] | null;
|
|
47
|
+
} | {
|
|
48
|
+
kind: 'point';
|
|
49
|
+
points: [number, number][];
|
|
50
|
+
};
|
|
51
|
+
/** The gestures of `<g9-canvas>` shared by every host: pointers in, `cv*` events and what to draw out. */
|
|
52
|
+
export declare class CanvasCore {
|
|
53
|
+
private readonly host;
|
|
54
|
+
readonly items: Map<string, CvItem>;
|
|
55
|
+
order: string[];
|
|
56
|
+
files: Record<string, string>;
|
|
57
|
+
view: CvView;
|
|
58
|
+
width: number;
|
|
59
|
+
height: number;
|
|
60
|
+
selection: CvSelection | null;
|
|
61
|
+
peers: CvPeer[];
|
|
62
|
+
hidden: Set<string>;
|
|
63
|
+
faded: Set<string>;
|
|
64
|
+
tool: CvTool;
|
|
65
|
+
editable: boolean;
|
|
66
|
+
stroke: CvStrokeStyle;
|
|
67
|
+
/** Bumped whenever the scene layer must be drawn again. */
|
|
68
|
+
sceneVersion: number;
|
|
69
|
+
/** Previews of finished gestures, until the app sends `done` with their id. */
|
|
70
|
+
readonly previews: Map<number, Preview>;
|
|
71
|
+
/** The live transform of the selection (finished gestures keep theirs until `done`). */
|
|
72
|
+
private live;
|
|
73
|
+
private readonly pointers;
|
|
74
|
+
private gesture;
|
|
75
|
+
private seq;
|
|
76
|
+
private lastTap;
|
|
77
|
+
private lastCursor;
|
|
78
|
+
private lastViewEmit;
|
|
79
|
+
private viewTimer;
|
|
80
|
+
eraserTrail: {
|
|
81
|
+
x: number;
|
|
82
|
+
y: number;
|
|
83
|
+
t: number;
|
|
84
|
+
}[];
|
|
85
|
+
/** Moving the camera with fingers right now (hosts draw the cached scene layer transformed). */
|
|
86
|
+
panning: boolean;
|
|
87
|
+
constructor(host: CoreHost);
|
|
88
|
+
apply(message: CvMessage): void;
|
|
89
|
+
setTool(tool: CvTool): void;
|
|
90
|
+
setSize(width: number, height: number): void;
|
|
91
|
+
toScene(x: number, y: number): [number, number];
|
|
92
|
+
toScreen(x: number, y: number): [number, number];
|
|
93
|
+
private setView;
|
|
94
|
+
/** Zoom about a point of the element (px). */
|
|
95
|
+
zoomAt(x: number, y: number, zoom: number): void;
|
|
96
|
+
/** Mouse wheel / trackpad: pan, or zoom with Ctrl (pinch on trackpads arrives as Ctrl+wheel). */
|
|
97
|
+
wheel(dx: number, dy: number, x: number, y: number, ctrl: boolean, shift: boolean): void;
|
|
98
|
+
private emitView;
|
|
99
|
+
/** The selection frame's corners in element px (rotated), or null. */
|
|
100
|
+
private framePolygon;
|
|
101
|
+
/** Handle positions in scene units (rotated with the frame); size depends on the pointer kind. */
|
|
102
|
+
handles(touch?: boolean): {
|
|
103
|
+
handle: CvHandle;
|
|
104
|
+
x: number;
|
|
105
|
+
y: number;
|
|
106
|
+
}[];
|
|
107
|
+
private insideFrame;
|
|
108
|
+
/** The live transform of the moving set (scene units), or null. */
|
|
109
|
+
liveMatrix(): Matrix | null;
|
|
110
|
+
/** Ids drawn by the overlay through `liveMatrix()` (not in the scene layer). */
|
|
111
|
+
movingIds(): ReadonlySet<string>;
|
|
112
|
+
/** The marquee box (scene) while dragging one. */
|
|
113
|
+
marquee(): CvBox | null;
|
|
114
|
+
/** The preview of the gesture in progress (plus finished ones waiting for the app). */
|
|
115
|
+
activePreviews(): Preview[];
|
|
116
|
+
pointerDown(input: PointerInput): void;
|
|
117
|
+
private moveStarted;
|
|
118
|
+
pointerMove(input: PointerInput): void;
|
|
119
|
+
pointerUp(input: PointerInput): void;
|
|
120
|
+
pointerCancel(id: number): void;
|
|
121
|
+
/** The mouse left the element. */
|
|
122
|
+
pointerLeave(): void;
|
|
123
|
+
private tap;
|
|
124
|
+
/** The app's answer to `cvdown`. */
|
|
125
|
+
private answer;
|
|
126
|
+
private handleAt;
|
|
127
|
+
private pointAt;
|
|
128
|
+
private startPinch;
|
|
129
|
+
private updatePinch;
|
|
130
|
+
private endCamera;
|
|
131
|
+
cancelGesture(): void;
|
|
132
|
+
private cursor;
|
|
133
|
+
/** The eraser trail's points (element px) still visible (they fade within 300 ms). */
|
|
134
|
+
trail(): {
|
|
135
|
+
x: number;
|
|
136
|
+
y: number;
|
|
137
|
+
t: number;
|
|
138
|
+
}[];
|
|
139
|
+
get busy(): boolean;
|
|
140
|
+
}
|
|
141
|
+
/** `point` turned by `angle` (radians) about `centre`. */
|
|
142
|
+
export declare function rotatePoint(point: [number, number], centre: [number, number], angle: number): [number, number];
|
|
143
|
+
/** The frame after dragging `handle` to `point` (the frame's own unrotated space; the opposite side stays). */
|
|
144
|
+
export declare function resizeBox(from: CvBox, angle: number, handle: CvHandle, point: [number, number], keepAspect: boolean): CvBox;
|
|
145
|
+
/** A matrix that moves by (`dx`, `dy`). */
|
|
146
|
+
export declare function translate(dx: number, dy: number): Matrix;
|
|
147
|
+
/** The matrix `m` after `n`. */
|
|
148
|
+
export declare function multiply(m: Matrix, n: Matrix): Matrix;
|
|
149
|
+
/** A matrix that turns by `angle` (radians) about a centre. */
|
|
150
|
+
export declare function rotateMatrix(angle: number, [cx, cy]: [number, number]): Matrix;
|
|
151
|
+
/** Maps the frame `from` onto `to` (both in the frame's space rotated by `angle` about from's centre). */
|
|
152
|
+
export declare function resizeMatrix(from: CvBox, to: CvBox, angle: number): Matrix;
|
|
153
|
+
/** A point through a matrix. */
|
|
154
|
+
export declare function applyMatrix(m: Matrix, [x, y]: [number, number]): [number, number];
|
|
155
|
+
//# sourceMappingURL=element-core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"element-core.d.ts","sourceRoot":"","sources":["../src/element-core.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,KAAK,EACL,QAAQ,EACR,MAAM,EACN,SAAS,EACT,MAAM,EACN,WAAW,EACX,aAAa,EACb,MAAM,EACN,MAAM,EACP,MAAM,YAAY,CAAC;AAMpB;;;;;;;;;;GAUG;AAEH,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE,MAAM,CAAC;IACX,oCAAoC;IACpC,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,IAAI,EAAE,OAAO,GAAG,OAAO,GAAG,KAAK,CAAC;IAChC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,GAAG,CAAC,EAAE,OAAO,CAAC;IACd,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AAED,4FAA4F;AAC5F,MAAM,WAAW,QAAQ;IACvB,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,GAAG,IAAI,CAAC;IAC1C,uEAAuE;IACvE,UAAU,IAAI,IAAI,CAAC;IACnB,GAAG,IAAI,MAAM,CAAC;CACf;AAED,oGAAoG;AACpG,MAAM,MAAM,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AAEtE,yGAAyG;AACzG,MAAM,MAAM,OAAO,GACf;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,IAAI,EAAE,WAAW,GAAG,SAAS,GAAG,SAAS,GAAG,OAAO,CAAC;IAAC,CAAC,EAAE,KAAK,CAAA;CAAE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