1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>SVG Test</title>
<style>
svg#gridSvg {
border: 2px solid rgb(255,0,0);
}
svg#gridSvg g > polygon {
fill:ghostwhite;
stroke: grey;
stroke-width: 0.4;
stroke-opacity:0.8;
}
svg#gridSvg polygon:hover {
fill :rgba(255,0,0,.5);
}
svg#gridSvg text {
font-family : Verdana;
font-size: 10px;
}
</style>
</head>
<body>
<svg id="gridSvg" width="1050px" height="800px" viewBox="0 0 1050 800" >
<defs>
<pattern id="image" height="100%" width="100%" patternContentUnits="objectBoundingBox" viewBox="0 0 1 1"
preserveAspectRatio="xMidYMid slice">
<image height="1" width="1" preserveAspectRatio="xMidYMid" xlink:href="img/tiles/grass1.png" />
</pattern>
</defs>
</svg>
<script defer>
const
svgns = "http://www.w3.org/2000/svg",
radius = 50,
cols = 50,
rows = 50,
offset = (Math.sqrt(3) * radius) / 2
;
var eGridSvg = {
ref : document.getElementById('gridSvg'),
vBx : { l : 0, t :0 , w:1050, h: 800 },
OnMove : false,
MovePos : { x :0, y : 0 }
};
function hexPoints(x, y, radius) {
let points = [];
for (let theta = 0; theta < Math.PI * 2; theta += Math.PI / 3) {
let pointX = x + radius * Math.sin(theta);
let pointY = y + radius * Math.cos(theta);
points.push(pointX.toFixed(2) + ',' + pointY.toFixed(2));
}
return points.join(' ');
}
for (let col = 0; col < cols; col++) {
for (let row = 0; row < rows; row++) {
let x = radius + offset * col * 2;
let y = radius + offset * row * Math.sqrt(3);
if (row % 2 !== 0) {
x += offset;
}
let g = document.createElementNS( svgns, 'g');
eGridSvg.ref.appendChild(g);
let poly = document.createElementNS( svgns, 'polygon');
poly.setAttributeNS(null, 'x', col);
poly.setAttributeNS(null, 'y', row);
poly.setAttributeNS(null, 'points', hexPoints(x, y, radius) );
g.appendChild(poly);
let txt = document.createElementNS( svgns, 'text');
txt.textContent = col + ':' + row;
txt.setAttributeNS(null,'x', hexPoints(x, y, radius / 2).split(' ')[0].split(',')[0]);
txt.setAttributeNS(null,'y', hexPoints(x, y, radius / 2).split(' ')[0].split(',')[1]);
g.appendChild(txt);
}
}
eGridSvg.ref.addEventListener('mousedown', function(e) {
eGridSvg.OnMove = true;
eGridSvg.MovePos = { x : e.clientX, y : e.clientY };
}, true);
document.addEventListener('mouseup', function() {
eGridSvg.OnMove = false;
}, true);
document.addEventListener('mousemove', function(event) {
event.preventDefault();
if (eGridSvg.OnMove) {
let mousePosition = { x : event.clientX, y : event.clientY };
eGridSvg.vBx.l -= (mousePosition.x - eGridSvg.MovePos.x);
eGridSvg.vBx.t -= (mousePosition.y - eGridSvg.MovePos.y);
eGridSvg.MovePos = mousePosition;
eGridSvg.ref.setAttribute("viewBox", `${eGridSvg.vBx.l} ${eGridSvg.vBx.t} ${eGridSvg.vBx.w} ${eGridSvg.vBx.h}`);
}
}, true);
</script>
</body>
</html> |
Partager