@jianghh/canvas-graffiti
Version:
canvas涂鸦库,支持手写、笔写、鼠标,选中元素,并且移动、删除等功能。集成撤销重做操作。
58 lines (57 loc) • 2.81 kB
JavaScript
import { isRectIntersect as l, genRectByTwoPoint as u } from "../element/index.js";
import { SYSTEM_COLOR as r } from "../index2.js";
import f from "../assets/cursor.png.js";
import { EleGroup as c } from "../element/group.js";
const m = {
buffer: !0,
pointerdown({ offsetX: i, offsetY: t }) {
var e;
this.bufferCtx.moveTo(i, t), this.bufferCtx.fillStyle = r, this.bufferCtx.strokeStyle = r, (e = this.eleGroup) != null && e.isSelected ? l(this.eleGroup, { left: i, top: t, right: i, bottom: t }) ? this.ctx.canvas.style.cursor = "move" : (this.ctx.canvas.style.cursor = "url(" + f + "), auto", this.eleGroup.cancelSelected()) : this.beginPoint = { x: i, y: t };
},
pointermove(i) {
var s, n, o;
const { offsetX: t, offsetY: e } = i;
(s = this.eleGroup) != null && s.isSelected ? (this.eleGroup.moveGroup(t - this.beginPoint.x, e - this.beginPoint.y), (o = (n = this.customizeHandle) == null ? void 0 : n.onGroupMoveHandle) == null || o.call(this, i), this.beginPoint = { x: t, y: e }) : (this.bufferCtx.clearRect(0, 0, this.el.width, this.el.height), this.bufferCtx.beginPath(), this.bufferCtx.moveTo(this.beginPoint.x, this.beginPoint.y), this.bufferCtx.rect(this.beginPoint.x, this.beginPoint.y, t - this.beginPoint.x, e - this.beginPoint.y), this.endPoint = { x: t, y: e }, this.bufferCtx.fill());
},
pointerup() {
var i, t, e, s, n;
if ((i = this.eleGroup) != null && i.isSelected)
this.ctx.canvas.style.cursor = "url(" + f + "), auto", (e = (t = this.customizeHandle) == null ? void 0 : t.onGroupHandle) == null || e.call(this, this.eleGroup), this.eleGroup.moveFinish(), this.emitStackChange();
else {
let o;
this.endPoint ? o = u(this.beginPoint, this.endPoint) : o = {
left: this.beginPoint.x - this.lineWidth,
top: this.beginPoint.y - this.lineWidth,
right: this.beginPoint.x + this.lineWidth,
bottom: this.beginPoint.y + this.lineWidth
}, a.call(this, o), (n = (s = this.customizeHandle) == null ? void 0 : s.onGroupHandle) == null || n.call(this, this.eleGroup);
}
}
};
function a(i) {
const t = this.graffitiEleList.filter((e) => {
if (l(e, i))
switch (e.tool) {
case "Marker":
return e.points.some((s, n) => {
const o = e.points[n + 1] || null;
let h;
if (o ? h = u(s, o) : h = {
left: s.x - this.lineWidth,
top: s.y - this.lineWidth,
right: s.x + this.lineWidth,
bottom: s.y + this.lineWidth
}, l(h, i))
return !0;
});
default:
return !0;
}
else
return !1;
});
this.eleGroup && this.eleGroup.cancelSelected(), t.length && (this.eleGroup = new c(this, t).selected());
}
export {
m as Cursor
};