|
| 1 | +/* |
| 2 | + * @Author: MOmo |
| 3 | + * @Date: 2024-07-12 15:10:02 |
| 4 | + * @LastEditors: MOmo |
| 5 | + * @LastEditTime: 2024-07-12 15:15:45 |
| 6 | + * @Description: 画布蒙层插件 |
| 7 | + */ |
| 8 | + |
| 9 | +import { fabric } from 'fabric'; |
| 10 | +import Editor from '../Editor'; |
| 11 | +type IEditor = Editor; |
| 12 | + |
| 13 | +class MaskPlugin implements IPluginTempl { |
| 14 | + static pluginName = 'MaskPlugin'; |
| 15 | + static apis = ['setCoverMask']; |
| 16 | + coverMask: null | fabric.Rect = null; |
| 17 | + workspace: null | fabric.Rect = null; |
| 18 | + workspaceEl!: HTMLElement; |
| 19 | + hackFlag = false; |
| 20 | + |
| 21 | + constructor(public canvas: fabric.Canvas, public editor: IEditor) { |
| 22 | + this.init(); |
| 23 | + } |
| 24 | + |
| 25 | + private init() { |
| 26 | + const workspaceEl = document.querySelector('#workspace') as HTMLElement; |
| 27 | + if (!workspaceEl) { |
| 28 | + throw new Error('element #workspace is missing, plz check!'); |
| 29 | + } |
| 30 | + this.workspaceEl = workspaceEl; |
| 31 | + this.initMask(); |
| 32 | + } |
| 33 | + |
| 34 | + // 返回workspace对象 |
| 35 | + getWorkspase() { |
| 36 | + return this.canvas.getObjects().find((item) => item.id === 'workspace') as fabric.Rect; |
| 37 | + } |
| 38 | + |
| 39 | + setCoverMask(hack = false) { |
| 40 | + if (!this.coverMask || !this.workspace) { |
| 41 | + return; |
| 42 | + } |
| 43 | + const center = this.canvas.getCenter(); |
| 44 | + const zoom = this.canvas.getZoom(); |
| 45 | + let zoomToPointNumber = zoom; |
| 46 | + if (hack) { |
| 47 | + // 比较hack的方法,判断为fabric内部的数据更新问题 |
| 48 | + zoomToPointNumber += 0.0000001 * (this.hackFlag ? 1 : -1); |
| 49 | + this.hackFlag = !this.hackFlag; |
| 50 | + } |
| 51 | + |
| 52 | + this.canvas.zoomToPoint(new fabric.Point(center.left, center.top), zoomToPointNumber); |
| 53 | + if (zoom) { |
| 54 | + const { workspaceEl } = this; |
| 55 | + const width = workspaceEl.offsetWidth; |
| 56 | + const height = workspaceEl.offsetHeight; |
| 57 | + const cWidth = width / zoom; |
| 58 | + const cHeight = height / zoom; |
| 59 | + this.coverMask.width = cWidth; |
| 60 | + this.coverMask.height = cHeight; |
| 61 | + this.coverMask.left = (this.workspace.left || 0) + (this.workspace.width! - cWidth) / 2; |
| 62 | + this.coverMask.top = (this.workspace.top || 0) + (this.workspace.height! - cHeight) / 2; |
| 63 | + this.workspace.clone((clone: fabric.Rect) => { |
| 64 | + clone.left = -clone.width! / 2; |
| 65 | + clone.top = -clone.height! / 2; |
| 66 | + clone.inverted = true; |
| 67 | + this.coverMask!.clipPath = clone; |
| 68 | + this.canvas.requestRenderAll(); |
| 69 | + }); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + initMask(needBindLoadJSON = true) { |
| 74 | + this.workspace = this.getWorkspase(); |
| 75 | + if (!this.workspace) { |
| 76 | + throw new Error('MaskPlugin must be used after WorkspacePlugin!'); |
| 77 | + } |
| 78 | + const coverMask = new fabric.Rect({ |
| 79 | + fill: 'rgba(0,0,0,0.7)', |
| 80 | + id: 'coverMask', |
| 81 | + strokeWidth: 0, |
| 82 | + }); |
| 83 | + coverMask.set('selectable', false); |
| 84 | + coverMask.set('hasControls', false); |
| 85 | + coverMask.set('evented', false); |
| 86 | + coverMask.hoverCursor = 'default'; |
| 87 | + this.canvas.on('object:added', () => { |
| 88 | + coverMask.bringToFront(); |
| 89 | + }); |
| 90 | + this.canvas.clipPath = undefined; |
| 91 | + this.canvas.add(coverMask); |
| 92 | + this.coverMask = coverMask; |
| 93 | + this.setCoverMask(); |
| 94 | + // 适配模板和psd的loadjson,在加载完成后再入mask |
| 95 | + needBindLoadJSON && this.editor.on('loadJson', () => this.initMask(false)); |
| 96 | + } |
| 97 | + |
| 98 | + destroy() { |
| 99 | + console.log('pluginDestroy'); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +export default MaskPlugin; |
0 commit comments