1 line
7.8 KiB
Plaintext
1 line
7.8 KiB
Plaintext
{"version":3,"file":"ext-grid.js","sources":["../../../src/editor/extensions/ext-grid.js"],"sourcesContent":["/**\n * @file ext-grid.js\n *\n * @license Apache-2.0\n *\n * @copyright 2010 Redou Mine, 2010 Alexis Deveria\n *\n */\n\nexport default {\n name: 'grid',\n async init ({$, NS, getTypeMap, importLocale}) {\n const strings = await importLocale();\n const svgEditor = this;\n const svgCanvas = svgEditor.canvas;\n const svgdoc = document.getElementById('svgcanvas').ownerDocument,\n {assignAttributes} = svgCanvas,\n hcanvas = document.createElement('canvas'),\n canvBG = $('#canvasBackground'),\n units = getTypeMap(), // Assumes prior `init()` call on `units.js` module\n intervals = [0.01, 0.1, 1, 10, 100, 1000];\n let showGrid = svgEditor.curConfig.showGrid || false;\n\n $(hcanvas).hide().appendTo('body');\n\n const canvasGrid = svgdoc.createElementNS(NS.SVG, 'svg');\n assignAttributes(canvasGrid, {\n id: 'canvasGrid',\n width: '100%',\n height: '100%',\n x: 0,\n y: 0,\n overflow: 'visible',\n display: 'none'\n });\n canvBG.append(canvasGrid);\n const gridDefs = svgdoc.createElementNS(NS.SVG, 'defs');\n // grid-pattern\n const gridPattern = svgdoc.createElementNS(NS.SVG, 'pattern');\n assignAttributes(gridPattern, {\n id: 'gridpattern',\n patternUnits: 'userSpaceOnUse',\n x: 0, // -(value.strokeWidth / 2), // position for strokewidth\n y: 0, // -(value.strokeWidth / 2), // position for strokewidth\n width: 100,\n height: 100\n });\n\n const gridimg = svgdoc.createElementNS(NS.SVG, 'image');\n assignAttributes(gridimg, {\n x: 0,\n y: 0,\n width: 100,\n height: 100\n });\n gridPattern.append(gridimg);\n gridDefs.append(gridPattern);\n $('#canvasGrid').append(gridDefs);\n\n // grid-box\n const gridBox = svgdoc.createElementNS(NS.SVG, 'rect');\n assignAttributes(gridBox, {\n width: '100%',\n height: '100%',\n x: 0,\n y: 0,\n 'stroke-width': 0,\n stroke: 'none',\n fill: 'url(#gridpattern)',\n style: 'pointer-events: none; display:visible;'\n });\n $('#canvasGrid').append(gridBox);\n\n /**\n *\n * @param {Float} zoom\n * @returns {void}\n */\n function updateGrid (zoom) {\n // TODO: Try this with <line> elements, then compare performance difference\n const unit = units[svgEditor.curConfig.baseUnit]; // 1 = 1px\n const uMulti = unit * zoom;\n // Calculate the main number interval\n const rawM = 100 / uMulti;\n let multi = 1;\n intervals.some((num) => {\n multi = num;\n return rawM <= num;\n });\n const bigInt = multi * uMulti;\n\n // Set the canvas size to the width of the container\n hcanvas.width = bigInt;\n hcanvas.height = bigInt;\n const ctx = hcanvas.getContext('2d');\n const curD = 0.5;\n const part = bigInt / 10;\n\n ctx.globalAlpha = 0.2;\n ctx.strokeStyle = svgEditor.curConfig.gridColor;\n for (let i = 1; i < 10; i++) {\n const subD = Math.round(part * i) + 0.5;\n // const lineNum = (i % 2)?12:10;\n const lineNum = 0;\n ctx.moveTo(subD, bigInt);\n ctx.lineTo(subD, lineNum);\n ctx.moveTo(bigInt, subD);\n ctx.lineTo(lineNum, subD);\n }\n ctx.stroke();\n ctx.beginPath();\n ctx.globalAlpha = 0.5;\n ctx.moveTo(curD, bigInt);\n ctx.lineTo(curD, 0);\n\n ctx.moveTo(bigInt, curD);\n ctx.lineTo(0, curD);\n ctx.stroke();\n\n const datauri = hcanvas.toDataURL('image/png');\n gridimg.setAttribute('width', bigInt);\n gridimg.setAttribute('height', bigInt);\n gridimg.parentNode.setAttribute('width', bigInt);\n gridimg.parentNode.setAttribute('height', bigInt);\n svgCanvas.setHref(gridimg, datauri);\n }\n\n /**\n *\n * @returns {void}\n */\n function gridUpdate () {\n if (showGrid) {\n updateGrid(svgCanvas.getZoom());\n }\n $('#canvasGrid').toggle(showGrid);\n $('#view_grid').toggleClass('push_button_pressed tool_button');\n }\n const buttons = [{\n id: 'view_grid',\n icon: 'grid.png',\n type: 'context',\n panel: 'editor_panel',\n events: {\n click () {\n svgEditor.curConfig.showGrid = showGrid = !showGrid;\n gridUpdate();\n }\n }\n }];\n return {\n name: strings.name,\n svgicons: 'grid-icon.xml',\n\n zoomChanged (zoom) {\n if (showGrid) { updateGrid(zoom); }\n },\n callback () {\n if (showGrid) {\n gridUpdate();\n }\n },\n buttons: strings.buttons.map((button, i) => {\n return Object.assign(buttons[i], button);\n })\n };\n }\n};\n"],"names":["name","init","updateGrid","gridUpdate","showGrid","svgCanvas","getZoom","$","toggle","toggleClass","zoom","uMulti","units","svgEditor","curConfig","baseUnit","rawM","multi","intervals","some","num","bigInt","hcanvas","width","height","ctx","getContext","curD","part","globalAlpha","strokeStyle","gridColor","i","subD","Math","round","moveTo","lineTo","stroke","beginPath","datauri","toDataURL","gridimg","setAttribute","parentNode","setHref","NS","getTypeMap","importLocale","strings","_this","canvas","svgdoc","document","getElementById","ownerDocument","assignAttributes","createElement","canvBG","hide","appendTo","canvasGrid","createElementNS","SVG","id","x","y","overflow","display","append","gridDefs","gridPattern","patternUnits","gridBox","fill","style","buttons","icon","type","panel","events","click","svgicons","zoomChanged","callback","map","button","Object","assign"],"mappings":";;;;;;;;;AASA,MAAe,CACbA,KAAM,OACAC,mZAmEKC,EAqDAC,oGAAAA,yBACHC,GACFF,EAAWG,EAAUC,WAEvBC,EAAE,eAAeC,OAAOJ,GACxBG,EAAE,cAAcE,YAAY,oCA1DrBP,uBAAYQ,OAGbC,EADOC,EAAMC,EAAUC,UAAUC,UACjBL,EAEhBM,EAAO,IAAML,EACfM,EAAQ,EACZC,EAAUC,MAAK,SAACC,UACdH,EAAQG,EACDJ,GAAQI,SAEXC,EAASJ,EAAQN,EAGvBW,EAAQC,MAAQF,EAChBC,EAAQE,OAASH,MACXI,EAAMH,EAAQI,WAAW,MACzBC,EAAO,GACPC,EAAOP,EAAS,GAEtBI,EAAII,YAAc,GAClBJ,EAAIK,YAAcjB,EAAUC,UAAUiB,cACjC,IAAIC,EAAI,EAAGA,EAAI,GAAIA,IAAK,KACrBC,EAAOC,KAAKC,MAAMP,EAAOI,GAAK,GAGpCP,EAAIW,OAA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