Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
32365812df | ||
|
880c0be4f1 | ||
|
0d6717896e |
13
bundle.js
13
bundle.js
@@ -390,6 +390,7 @@ class Doodler {
|
|||||||
mouseX = 0;
|
mouseX = 0;
|
||||||
mouseY = 0;
|
mouseY = 0;
|
||||||
registerDraggable(point, radius, style) {
|
registerDraggable(point, radius, style) {
|
||||||
|
if (this.draggables.find((d)=>d.point === point)) return;
|
||||||
const id = this.addUIElement('circle', point, radius, {
|
const id = this.addUIElement('circle', point, radius, {
|
||||||
fillColor: '#5533ff50',
|
fillColor: '#5533ff50',
|
||||||
strokeColor: '#5533ff50'
|
strokeColor: '#5533ff50'
|
||||||
@@ -409,19 +410,25 @@ class Doodler {
|
|||||||
}
|
}
|
||||||
this.draggables = this.draggables.filter((d)=>d.point !== point);
|
this.draggables = this.draggables.filter((d)=>d.point !== point);
|
||||||
}
|
}
|
||||||
|
addDragEvents({ onDragEnd , onDragStart , point }) {
|
||||||
|
const d = this.draggables.find((d)=>d.point === point);
|
||||||
|
if (d) {
|
||||||
|
d.onDragEnd = onDragEnd;
|
||||||
|
d.onDragStart = onDragStart;
|
||||||
|
}
|
||||||
|
}
|
||||||
onClick(e) {
|
onClick(e) {
|
||||||
const rect = this._canvas.getBoundingClientRect();
|
|
||||||
e.clientX - rect.left;
|
|
||||||
e.clientY - rect.top;
|
|
||||||
for (const d of this.draggables){
|
for (const d of this.draggables){
|
||||||
if (d.point.dist(new Vector(this.mouseX, this.mouseY)) <= d.radius) {
|
if (d.point.dist(new Vector(this.mouseX, this.mouseY)) <= d.radius) {
|
||||||
d.beingDragged = true;
|
d.beingDragged = true;
|
||||||
|
d.onDragStart?.call(null);
|
||||||
} else d.beingDragged = false;
|
} else d.beingDragged = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
offClick(e) {
|
offClick(e) {
|
||||||
for (const d of this.draggables){
|
for (const d of this.draggables){
|
||||||
d.beingDragged = false;
|
d.beingDragged = false;
|
||||||
|
d.onDragEnd?.call(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
uiElements = new Map();
|
uiElements = new Map();
|
||||||
|
31
canvas.ts
31
canvas.ts
@@ -209,6 +209,7 @@ export class Doodler {
|
|||||||
|
|
||||||
|
|
||||||
registerDraggable(point: Vector, radius: number, style?: IStyle & { shape: 'square' | 'circle' }) {
|
registerDraggable(point: Vector, radius: number, style?: IStyle & { shape: 'square' | 'circle' }) {
|
||||||
|
if (this.draggables.find(d => d.point === point)) return;
|
||||||
const id = this.addUIElement('circle', point, radius, { fillColor: '#5533ff50', strokeColor: '#5533ff50' })
|
const id = this.addUIElement('circle', point, radius, { fillColor: '#5533ff50', strokeColor: '#5533ff50' })
|
||||||
this.draggables.push({ point, radius, style, id });
|
this.draggables.push({ point, radius, style, id });
|
||||||
}
|
}
|
||||||
@@ -221,21 +222,31 @@ export class Doodler {
|
|||||||
this.draggables = this.draggables.filter(d => d.point !== point);
|
this.draggables = this.draggables.filter(d => d.point !== point);
|
||||||
}
|
}
|
||||||
|
|
||||||
onClick(e: MouseEvent) {
|
addDragEvents({
|
||||||
const rect = this._canvas.getBoundingClientRect();
|
onDragEnd,
|
||||||
const mouseX = e.clientX - rect.left;
|
onDragStart,
|
||||||
const mouseY = e.clientY - rect.top;
|
point
|
||||||
|
}: {point: Vector, onDragEnd?: () => void, onDragStart?: () => void}) {
|
||||||
for (const d of this.draggables) {
|
const d = this.draggables.find(d =>d.point === point);
|
||||||
if (d.point.dist(new Vector(this.mouseX, this.mouseY)) <= d.radius) {
|
if (d) {
|
||||||
d.beingDragged = true;
|
d.onDragEnd = onDragEnd;
|
||||||
} else d.beingDragged = false;
|
d.onDragStart = onDragStart;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onClick(e: MouseEvent) {
|
||||||
|
for (const d of this.draggables) {
|
||||||
|
if (d.point.dist(new Vector(this.mouseX, this.mouseY)) <= d.radius) {
|
||||||
|
d.beingDragged = true;
|
||||||
|
d.onDragStart?.call(null);
|
||||||
|
} else d.beingDragged = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
offClick(e:MouseEvent){
|
offClick(e:MouseEvent){
|
||||||
for (const d of this.draggables) {
|
for (const d of this.draggables) {
|
||||||
d.beingDragged = false;
|
d.beingDragged = false;
|
||||||
|
d.onDragEnd?.call(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -299,6 +310,8 @@ type Draggable = {
|
|||||||
style?: IStyle & { shape: 'square' | 'circle' };
|
style?: IStyle & { shape: 'square' | 'circle' };
|
||||||
beingDragged?: boolean;
|
beingDragged?: boolean;
|
||||||
id: string;
|
id: string;
|
||||||
|
onDragStart?: () => void;
|
||||||
|
onDragEnd?: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
type uiDrawing = {
|
type uiDrawing = {
|
||||||
|
Reference in New Issue
Block a user