move pattern definition back into (#65)

* .gitignore

* move pattern definition back into js (#64)

Co-authored-by: finnboeger <finnboeger@users.noreply.github.com>
This commit is contained in:
Ophir LOJKINE 2020-05-04 09:54:31 +02:00 committed by GitHub
parent bb0f5aaf51
commit 46c2737c00
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 67 additions and 26 deletions

4
.gitignore vendored
View file

@ -17,9 +17,7 @@ npm-debug.log
.DS_Store
# Openode
.openode
wbo-backup.zip
wbo-backup*
# Jetbrains
.idea/

View file

@ -498,8 +498,13 @@ Tools.generateUID = function (prefix, suffix) {
return uid;
};
Tools.createSVGElement = function (name) {
return document.createElementNS(Tools.svg.namespaceURI, name);
Tools.createSVGElement = function createSVGElement(name, attrs) {
var elem = document.createElementNS(Tools.svg.namespaceURI, name);
if (typeof(attrs) !== "object") return elem;
Object.keys(attrs).forEach(function (key, i) {
elem.setAttributeNS(null, key, attrs[key]);
});
return elem;
};
Tools.positionElement = function (elem, x, y) {

View file

@ -27,24 +27,76 @@
(function grid() { //Code isolation
var index = 0; //grid off by default
var states = ["none", "url(../tools/grid/patterns.svg#grid)", "url(../tools/grid/patterns.svg#dots)"];
var states = ["none", "url(#grid)", "url(#dots)"];
function toggleGrid(evt) {
index = (index + 1) % states.length;
gridContainer.setAttributeNS(null, "fill", states[index]);
}
function createSVGElement(name, attrs) {
var elem = document.createElementNS("http://www.w3.org/2000/svg", name);
Object.keys(attrs).forEach(function (key, i) {
elem.setAttributeNS(null, key, attrs[key]);
function createPatterns() {
// create patterns
// small (inner) grid
var smallGrid = Tools.createSVGElement("pattern", {
id: "smallGrid",
width: "30",
height: "30",
patternUnits: "userSpaceOnUse"
});
return elem;
smallGrid.appendChild(
Tools.createSVGElement("path", {
d: "M 30 0 L 0 0 0 30",
fill: "none",
stroke: "gray",
'stroke-width': "0.5"
})
);
// (outer) grid
var grid = Tools.createSVGElement("pattern", {
id: "grid",
width: "300",
height: "300",
patternUnits: "userSpaceOnUse"
});
grid.appendChild(Tools.createSVGElement("rect", {
width: "300",
height: "300",
fill: "url(#smallGrid)"
}));
grid.appendChild(
Tools.createSVGElement("path", {
d: "M 300 0 L 0 0 0 300",
fill: "none",
stroke: "gray", 'stroke-width': "1"
})
);
// dots
var dots = Tools.createSVGElement("pattern", {
id: "dots",
width: "30",
height: "30",
x: "-10",
y: "-10",
patternUnits: "userSpaceOnUse"
});
dots.appendChild(Tools.createSVGElement("circle", {
fill: "gray",
cx: "10",
cy: "10",
r: "2"
}));
var defs = Tools.svg.getElementById("defs");
defs.appendChild(smallGrid);
defs.appendChild(grid);
defs.appendChild(dots);
}
var gridContainer = (function init() {
// initialize patterns
createPatterns();
// create grid container
var gridContainer = createSVGElement("rect", {
var gridContainer = Tools.createSVGElement("rect", {
id: "gridContainer",
width: "100%", height: "100%",
fill: states[index]

View file

@ -1,14 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg">
<defs id="defs">
<pattern id="smallGrid" width="30" height="30" patternUnits="userSpaceOnUse">
<path d="M 30 0 L 0 0 0 30" fill="none" stroke="gray" stroke-width="0.5"/>
</pattern>
<pattern id="grid" width="300" height="300" patternUnits="userSpaceOnUse">
<rect width="300" height="300" fill="url(#smallGrid)"/>
<path d="M 300 0 L 0 0 0 300" fill="none" stroke="gray" stroke-width="1"/>
</pattern>
<pattern id="dots" width="30" height="30" x="-10" y="-10" patternUnits="userSpaceOnUse">
<circle fill="gray" cx="10" cy="10" r="2"/>
</pattern>
</defs>
</svg>

Before

Width:  |  Height:  |  Size: 701 B