1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| const parameter = { fillStyle: '#8f8ffa40', strokeStyle: '#0063ff', radius: 4, };
interface Point { x: number; y: number; }
export class CanvasEdit { private _points: Point[];
private _canvas: HTMLCanvasElement;
private _ctx: CanvasRenderingContext2D;
private _pickIndex: number | undefined;
private _draw: boolean;
public uvUpdateEvent: Event;
constructor(canvas: HTMLCanvasElement) { this._points = []; this._canvas = canvas; canvas.oncontextmenu = (ev) => { ev.preventDefault(); return false; }; this._ctx = canvas.getContext('2d') as CanvasRenderingContext2D; this._canvas.addEventListener('mousedown', this._mousedownHandler.bind(this)); this._canvas.addEventListener('mouseup', () => { this._pickIndex = undefined; }); this._canvas.addEventListener('mousemove', this._mousemoveHandler.bind(this)); this._draw = false; this._pickIndex = undefined; }
draw() { this.clear(); this._draw = true; this._pickIndex = undefined; }
edit() { this._draw = false; }
clear() { this._points = []; this._update(); }
setPointsRorUvs(uvs: number[][]) { this._points = []; uvs.forEach((uv) => { this._points.push({ x: uv[0] * this._canvas.width, y: this._canvas.height - uv[1] * this._canvas.height, }); }); this._update(); }
private _mousemoveHandler(event) { if (this._pickIndex !== undefined) { this._points[this._pickIndex] = { x: event.offsetX, y: event.offsetY, }; this._update(); } }
private _mousedownHandler(event) { if (event.button === 0) { if (this._draw) { this._points.push({ x: event.offsetX, y: event.offsetY, }); this._update(); } else { for (let i = 0; i < this._points.length; i++) { const x = this._points[i].x - event.offsetX; const y = this._points[i].y - event.offsetY; const distance = Math.sqrt(x * x + y * y); if (distance <= parameter.radius) { this._pickIndex = i; break; } } } } }
private _update() { this._ctx.clearRect(0, 0, this._canvas.width, this._canvas.height);
if (this._points.length > 0) { this._ctx.fillStyle = parameter.fillStyle; this._ctx.strokeStyle = parameter.strokeStyle;
this._ctx.beginPath(); this._ctx.moveTo(this._points[0].x, this._points[0].y); for (let i = 1; i < this._points.length; i++) { this._ctx.lineTo(this._points[i].x, this._points[i].y); } this._ctx.closePath(); this._ctx.stroke(); this._ctx.fill();
this._points.forEach((point) => { this._ctx.beginPath(); this._ctx.arc(point.x, point.y, parameter.radius, 0, 2 * Math.PI); this._ctx.stroke(); }); } console.log('uv集合:', this._getUvForPoints()); }
private _getUvForPoints() { return this._points.map((p) => [ p.x / this._canvas.width, (this._canvas.height - p.y) / this._canvas.height, ]); } }
|