Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
6530b928f5 | ||
|
32365812df | ||
|
880c0be4f1 |
@@ -410,16 +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) {
|
||||||
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();
|
||||||
|
43
canvas.ts
43
canvas.ts
@@ -37,6 +37,7 @@ export class Doodler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private draggables: Draggable[] = [];
|
private draggables: Draggable[] = [];
|
||||||
|
private clickables: Clickable[] = [];
|
||||||
|
|
||||||
constructor({
|
constructor({
|
||||||
width,
|
width,
|
||||||
@@ -222,17 +223,50 @@ export class Doodler {
|
|||||||
this.draggables = this.draggables.filter(d => d.point !== point);
|
this.draggables = this.draggables.filter(d => d.point !== point);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
registerClickable(p1: Vector, p2: Vector, cb: () => void) {
|
||||||
|
const top = Math.min(p1.y, p2.y);
|
||||||
|
const left = Math.min(p1.x, p2.x);
|
||||||
|
const bottom = Math.max(p1.y, p2.y);
|
||||||
|
const right = Math.max(p1.x, p2.x);
|
||||||
|
|
||||||
|
this.clickables.push({
|
||||||
|
onClick: cb,
|
||||||
|
checkBound: (p) => p.y >= top && p.x >= left && p.y <= bottom && p.x <= right
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
addDragEvents({
|
||||||
|
onDragEnd,
|
||||||
|
onDragStart,
|
||||||
|
point
|
||||||
|
}: { point: Vector, onDragEnd?: () => void, onDragStart?: () => void }) {
|
||||||
|
const d = this.draggables.find(d => d.point === point);
|
||||||
|
if (d) {
|
||||||
|
d.onDragEnd = onDragEnd;
|
||||||
|
d.onDragStart = onDragStart;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
onClick(e: MouseEvent) {
|
onClick(e: MouseEvent) {
|
||||||
|
const mouse = new Vector(this.mouseX, this.mouseY)
|
||||||
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(mouse) <= d.radius) {
|
||||||
d.beingDragged = true;
|
d.beingDragged = true;
|
||||||
|
d.onDragStart?.call(null);
|
||||||
} else d.beingDragged = false;
|
} else d.beingDragged = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (const c of this.clickables) {
|
||||||
|
if (c.checkBound(mouse)) {
|
||||||
|
c.onClick();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -296,6 +330,13 @@ 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 Clickable = {
|
||||||
|
onClick: () => void;
|
||||||
|
checkBound: (p: Vector) => boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
type uiDrawing = {
|
type uiDrawing = {
|
||||||
|
Reference in New Issue
Block a user