svgedit/dist/editor/extensions/ext-grid/ext-grid.js.map

1 line
7.9 KiB
Plaintext
Raw Normal View History

2020-09-06 06:32:10 +00:00
{"version":3,"file":"ext-grid.js","sources":["../../../../src/editor/extensions/ext-grid/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}) {\n const svgEditor = this;\n // eslint-disable-next-line node/no-unsupported-features/es-syntax\n const {default: strings} = await import(`./locale/${svgEditor.curPrefs.lang}.js`);\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 {voi