2018-05-16 00:53:27 +00:00
|
|
|
/* eslint-disable no-var */
|
|
|
|
/* globals svgEditor, svgedit, svgCanvas, $ */
|
2010-09-09 12:46:02 +00:00
|
|
|
/*
|
|
|
|
* ext-grid.js
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2
|
|
|
|
*
|
|
|
|
* Copyright(c) 2010 Redou Mine
|
2010-10-21 17:02:12 +00:00
|
|
|
* Copyright(c) 2010 Alexis Deveria
|
2010-09-09 12:46:02 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-11-06 00:45:21 +00:00
|
|
|
// Dependencies:
|
|
|
|
// 1) units.js
|
|
|
|
// 2) everything else
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgEditor.addExtension('view_grid', function () {
|
|
|
|
'use strict';
|
2013-02-16 11:10:26 +00:00
|
|
|
|
2013-02-16 15:02:26 +00:00
|
|
|
var NS = svgedit.NS,
|
|
|
|
svgdoc = document.getElementById('svgcanvas').ownerDocument,
|
2014-04-09 01:50:52 +00:00
|
|
|
showGrid = svgEditor.curConfig.showGrid || false,
|
2013-02-16 11:10:26 +00:00
|
|
|
assignAttributes = svgCanvas.assignAttributes,
|
|
|
|
hcanvas = document.createElement('canvas'),
|
|
|
|
canvBG = $('#canvasBackground'),
|
|
|
|
units = svgedit.units.getTypeMap(),
|
|
|
|
intervals = [0.01, 0.1, 1, 10, 100, 1000];
|
|
|
|
|
|
|
|
$(hcanvas).hide().appendTo('body');
|
|
|
|
|
2013-02-16 15:02:26 +00:00
|
|
|
var canvasGrid = svgdoc.createElementNS(NS.SVG, 'svg');
|
2013-02-16 11:10:26 +00:00
|
|
|
assignAttributes(canvasGrid, {
|
|
|
|
'id': 'canvasGrid',
|
|
|
|
'width': '100%',
|
|
|
|
'height': '100%',
|
|
|
|
'x': 0,
|
|
|
|
'y': 0,
|
|
|
|
'overflow': 'visible',
|
|
|
|
'display': 'none'
|
|
|
|
});
|
|
|
|
canvBG.append(canvasGrid);
|
|
|
|
|
|
|
|
// grid-pattern
|
2013-02-16 15:02:26 +00:00
|
|
|
var gridPattern = svgdoc.createElementNS(NS.SVG, 'pattern');
|
2013-02-16 11:10:26 +00:00
|
|
|
assignAttributes(gridPattern, {
|
|
|
|
'id': 'gridpattern',
|
|
|
|
'patternUnits': 'userSpaceOnUse',
|
2018-05-16 00:53:27 +00:00
|
|
|
'x': 0, // -(value.strokeWidth / 2), // position for strokewidth
|
|
|
|
'y': 0, // -(value.strokeWidth / 2), // position for strokewidth
|
2013-02-16 11:10:26 +00:00
|
|
|
'width': 100,
|
|
|
|
'height': 100
|
|
|
|
});
|
|
|
|
|
2013-02-16 15:02:26 +00:00
|
|
|
var gridimg = svgdoc.createElementNS(NS.SVG, 'image');
|
2013-02-16 11:10:26 +00:00
|
|
|
assignAttributes(gridimg, {
|
|
|
|
'x': 0,
|
|
|
|
'y': 0,
|
|
|
|
'width': 100,
|
|
|
|
'height': 100
|
|
|
|
});
|
|
|
|
gridPattern.appendChild(gridimg);
|
|
|
|
$('#svgroot defs').append(gridPattern);
|
|
|
|
|
|
|
|
// grid-box
|
2013-02-16 15:02:26 +00:00
|
|
|
var gridBox = svgdoc.createElementNS(NS.SVG, 'rect');
|
2013-02-16 11:10:26 +00:00
|
|
|
assignAttributes(gridBox, {
|
|
|
|
'width': '100%',
|
|
|
|
'height': '100%',
|
|
|
|
'x': 0,
|
|
|
|
'y': 0,
|
|
|
|
'stroke-width': 0,
|
|
|
|
'stroke': 'none',
|
|
|
|
'fill': 'url(#gridpattern)',
|
|
|
|
'style': 'pointer-events: none; display:visible;'
|
|
|
|
});
|
|
|
|
$('#canvasGrid').append(gridBox);
|
2010-10-21 17:02:12 +00:00
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
function updateGrid (zoom) {
|
2014-02-12 09:38:38 +00:00
|
|
|
var i;
|
2010-10-21 17:02:12 +00:00
|
|
|
// TODO: Try this with <line> elements, then compare performance difference
|
|
|
|
var unit = units[svgEditor.curConfig.baseUnit]; // 1 = 1px
|
2018-05-16 00:53:27 +00:00
|
|
|
var uMulti = unit * zoom;
|
2010-10-21 17:02:12 +00:00
|
|
|
// Calculate the main number interval
|
2018-05-16 00:53:27 +00:00
|
|
|
var rawM = 100 / uMulti;
|
2010-10-21 17:02:12 +00:00
|
|
|
var multi = 1;
|
2014-02-12 09:38:38 +00:00
|
|
|
for (i = 0; i < intervals.length; i++) {
|
2013-02-16 11:10:26 +00:00
|
|
|
var num = intervals[i];
|
2010-10-21 17:02:12 +00:00
|
|
|
multi = num;
|
2018-05-16 00:53:27 +00:00
|
|
|
if (rawM <= num) {
|
2010-10-21 17:02:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-05-16 00:53:27 +00:00
|
|
|
var bigInt = multi * uMulti;
|
2010-10-21 17:02:12 +00:00
|
|
|
|
|
|
|
// Set the canvas size to the width of the container
|
2018-05-16 00:53:27 +00:00
|
|
|
hcanvas.width = bigInt;
|
|
|
|
hcanvas.height = bigInt;
|
2013-02-16 11:10:26 +00:00
|
|
|
var ctx = hcanvas.getContext('2d');
|
2018-05-16 00:53:27 +00:00
|
|
|
var curD = 0.5;
|
|
|
|
var part = bigInt / 10;
|
2010-10-21 17:02:12 +00:00
|
|
|
|
|
|
|
ctx.globalAlpha = 0.2;
|
2013-02-19 21:07:24 +00:00
|
|
|
ctx.strokeStyle = svgEditor.curConfig.gridColor;
|
2014-02-12 09:38:38 +00:00
|
|
|
for (i = 1; i < 10; i++) {
|
2018-05-16 00:53:27 +00:00
|
|
|
var subD = Math.round(part * i) + 0.5;
|
|
|
|
// var lineNum = (i % 2)?12:10;
|
|
|
|
var lineNum = 0;
|
|
|
|
ctx.moveTo(subD, bigInt);
|
|
|
|
ctx.lineTo(subD, lineNum);
|
|
|
|
ctx.moveTo(bigInt, subD);
|
|
|
|
ctx.lineTo(lineNum, subD);
|
2010-10-21 17:02:12 +00:00
|
|
|
}
|
|
|
|
ctx.stroke();
|
2013-02-16 11:10:26 +00:00
|
|
|
ctx.beginPath();
|
2010-10-21 17:02:12 +00:00
|
|
|
ctx.globalAlpha = 0.5;
|
2018-05-16 00:53:27 +00:00
|
|
|
ctx.moveTo(curD, bigInt);
|
|
|
|
ctx.lineTo(curD, 0);
|
2010-10-21 17:02:12 +00:00
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
ctx.moveTo(bigInt, curD);
|
|
|
|
ctx.lineTo(0, curD);
|
2010-10-21 17:02:12 +00:00
|
|
|
ctx.stroke();
|
|
|
|
|
2013-02-16 11:10:26 +00:00
|
|
|
var datauri = hcanvas.toDataURL('image/png');
|
2018-05-16 00:53:27 +00:00
|
|
|
gridimg.setAttribute('width', bigInt);
|
|
|
|
gridimg.setAttribute('height', bigInt);
|
|
|
|
gridimg.parentNode.setAttribute('width', bigInt);
|
|
|
|
gridimg.parentNode.setAttribute('height', bigInt);
|
2010-10-21 17:02:12 +00:00
|
|
|
svgCanvas.setHref(gridimg, datauri);
|
|
|
|
}
|
2010-09-09 12:46:02 +00:00
|
|
|
|
2014-04-09 02:25:25 +00:00
|
|
|
function gridUpdate () {
|
|
|
|
if (showGrid) {
|
|
|
|
updateGrid(svgCanvas.getZoom());
|
|
|
|
}
|
|
|
|
$('#canvasGrid').toggle(showGrid);
|
|
|
|
$('#view_grid').toggleClass('push_button_pressed tool_button');
|
|
|
|
}
|
2013-02-16 11:10:26 +00:00
|
|
|
return {
|
|
|
|
name: 'view_grid',
|
2014-02-12 03:48:48 +00:00
|
|
|
svgicons: svgEditor.curConfig.extPath + 'grid-icon.xml',
|
2013-02-16 11:10:26 +00:00
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
zoomChanged: function (zoom) {
|
|
|
|
if (showGrid) { updateGrid(zoom); }
|
2013-02-16 11:10:26 +00:00
|
|
|
},
|
2014-04-09 02:25:25 +00:00
|
|
|
callback: function () {
|
|
|
|
if (showGrid) {
|
|
|
|
gridUpdate();
|
2014-04-09 03:44:19 +00:00
|
|
|
}
|
2014-04-09 02:25:25 +00:00
|
|
|
},
|
2013-02-16 11:10:26 +00:00
|
|
|
buttons: [{
|
|
|
|
id: 'view_grid',
|
|
|
|
type: 'context',
|
|
|
|
panel: 'editor_panel',
|
|
|
|
title: 'Show/Hide Grid',
|
|
|
|
events: {
|
2018-05-16 00:53:27 +00:00
|
|
|
click: function () {
|
2013-02-16 11:10:26 +00:00
|
|
|
svgEditor.curConfig.showGrid = showGrid = !showGrid;
|
2014-04-09 02:25:25 +00:00
|
|
|
gridUpdate();
|
2013-02-16 11:10:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
});
|