Looking for some game ideas? Why not take a look at the JS 13K 2020 competition top 10.
They all look brilliant to me.
Looking for some game ideas? Why not take a look at the JS 13K 2020 competition top 10.
They all look brilliant to me.
html { padding: 0; margin: 0; border: 0;}
#dvViewer {
width: 300px;
height: 200px;
}
svg {
height: 100%;
width: 100%;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible">
<title>Basic SVG Demo</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
</head>
<body>
<div id="dvViewer">
<svg viewBox="0 0 200 100" xmlns="http://www.w3.org/2000/svg">
<style>
.normal { font: normal 15px sans-serif; }
.bold { font: bold 30px sans-serif; }
.bright { font: italic 40px serif; fill: red; }
</style> <!-- font colour is the fill -->
<text x="10" y="35" class="normal">Welcome</text>
<text x="75" y="35" class="bold">readers</text>
<text x="55" y="65" class="normal">to</text>
<text x="70" y="65" class="bright">SVG!</text>
</svg>
</div>
</body>
</html>
<div id="dvScale">
<svg viewBox="0 0 510 30" xmlns="http://www.w3.org/2000/svg">
<style>
.normals { font: normal 10px sans-serif; text-anchor: middle;}
.tl {stroke: black; stroke-width: 0.5;}
</style>
<line x1="5" y1="20" x2="5" y2="30" class="tl" />
<text x="5" y="19" class="normals" >1</text>
<line x1="155.5" y1="20" x2="155.5" y2="30" class="tl" />
<text x="155.5" y="19" class="normals" >2</text>
<line x1="243.5" y1="20" x2="243.5" y2="30" class="tl" />
<text x="243.5" y="19" class="normals" >3</text>
<line x1="306" y1="20" x2="306" y2="30" class="tl" />
<text x="306" y="19" class="normals" >4</text>
<line x1="354.5" y1="20" x2="354.5" y2="30" class="tl" />
<text x="354.5" y="19" class="normals" >5</text>
<line x1="394" y1="20" x2="394" y2="30" class="tl" />
<text x="394" y="19" class="normals" >6</text>
<line x1="427.5" y1="20" x2="427.5" y2="30" class="tl" />
<text x="427.5" y="19" class="normals" >7</text>
<line x1="456.5" y1="20" x2="456.5" y2="30" class="tl" />
<text x="456.5" y="19" class="normals" >8</text>
<line x1="482" y1="20" x2="482" y2="30" class="tl" />
<text x="482" y="19" class="normals" >9</text>
<line x1="505" y1="20" x2="505" y2="30" class="tl" />
<text x="505" y="19" class="normals" >10</text>
</svg>
</div>
#dvScale {
width: 510px;
height: 30px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible">
<title>SVG and JavaScript</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<script src="svg.js"></script>
</head>
<body>
<div id="dvScale">
</div>
<script src="main.js"></script>
</body>
</html>
function createSvgElem(svgTag, svgAttr, svgStyle, addTo) {
const nSpace = "http://www.w3.org/2000/svg";
let nElem = document.createElementNS(nSpace, svgTag);
for (let atrib in svgAttr) {
nElem.setAttributeNS(null, atrib, svgAttr[atrib]);
}
if (svgStyle) {
for (let atrib in svgStyle) {
nElem.style[atrib] = svgStyle[atrib];
}
}
if (addTo) {
addTo.appendChild(nElem);
}
return nElem;
}
var div = document.getElementById("dvScale");
function initialise(){
let svgStyle = {
small: { "font": "normal 10px sans-serif", "text-anchor": "middle"},
tl: {"stroke": "black", "stroke-width": "0.5"}
};
let svgAttr = {"viewBox": "0 0 510 30"};
let svg = createSvgElem("svg", svgAttr, null, div);
for(let i = 1; i < 11; i++){
let x = (Math.log10(i) * 500 + 5).toFixed(1); // log of number * scale length + scale start
svgAttr = {"x1": x, "x2": x, "y1": "20", "y2": "30"};
createSvgElem("line", svgAttr, svgStyle.tl, svg);
let lab = createSvgElem("text", {"x": x, "y": "19"}, svgStyle.small, svg);
lab.innerHTML = i.toString();
}
}
initialise();
var div = document.getElementById("dvScale");
function initialise(){
let svgAttr = {"viewBox": "0 0 510 30"};
let svg = createSvgElem("svg", svgAttr, null, div);
svgAttr = {x: 2, y: 2, width: 30, height: 20};
createSvgElem("rect", svgAttr, null, svg);
}
initialise();
var div = document.getElementById("dvScale");
function initialise(){
let svgAttr = {"viewBox": "0 0 510 30"};
let svg = createSvgElem("svg", svgAttr, null, div);
svgAttr = {x: 2, y: 2, width: 30, height: 20};
let svgStyle = {stroke: "lime", fill: "white"};
createSvgElem("rect", svgAttr, svgStyle, svg);
}
initialise();
let svgStyle = {stroke: "lime", fill: "transparent", "stroke-width": 3};
let svgStyle = {stroke: "black", fill: "transparent", "stroke-width": 3, rx: 5, ry: 5};
var div = document.getElementById("dvScale");
function initialise(){
let svgAttr = {"viewBox": "0 0 510 30"};
let svg = createSvgElem("svg", svgAttr, null, div);
svgAttr = {cx: 20, cy: 15, r: 12};
let svgStyle = {stroke: "hotpink", fill: "transparent", "stroke-width": 1};
createSvgElem("circle", svgAttr, svgStyle, svg);
svgAttr = {cx: 60, cy: 15, rx: 12, ry: 6};
createSvgElem("ellipse", svgAttr, svgStyle, svg);
}
initialise();
var div = document.getElementById("dvScale");
function initialise(){
let svgAttr = {"viewBox": "0 0 510 30"};
let svg = createSvgElem("svg", svgAttr, null, div);
svgAttr = {points: "0 0, 10 10, 0 20, 30 20, 40 10"};
let svgStyle = {stroke: "teal", "stroke-width": 1};
createSvgElem("polyline", svgAttr, svgStyle, svg);
}
initialise();
let svgStyle = {stroke: "teal", "stroke-width": 1, fill: "transparent"};
var div = document.getElementById("dvScale");
function initialise(){
let svgAttr = {"viewBox": "0 0 510 30"};
let svg = createSvgElem("svg", svgAttr, null, div);
svgAttr = {points: "15 0, 0 10, 5 30, 25 30, 30 10"};
let svgStyle = {stroke: "red", "stroke-width": 1, fill: "transparent"};
createSvgElem("polygon", svgAttr, svgStyle, svg);
}
initialise();
var div = document.getElementById("dvScale");
function initialise(){
let svgAttr = {"viewBox": "0 0 510 60"};
let svg = createSvgElem("svg", svgAttr, null, div);
let svgStyle = {stroke: "red", "stroke-width": 1, fill: "transparent"};
svgAttr = {d: "m31.847256,20.147449c7.627459,-16.538212 37.512093,0 0,21.263415c-37.512093,-21.263415 \
-7.627459,-37.801627 0,-21.263415z"};
createSvgElem("path", svgAttr, svgStyle, svg);
svgAttr = {d: "m74.805598,52.920259c-1.325839,-1.078225 2.076708,-1.107086 \
1.542153,-2.302318c0.977352,-1.583842 3.518568,-2.542536 3.892802,-4.342332c-1.030665,-0.500215 \
-1.623434,-1.709964 0.342901,-1.566342c1.44199,-0.756148 1.261771,-2.303513 1.695423,-3.439055 \
c0.40623,-1.83279 0.498224,-3.687829 0.67271,-5.535677c-1.668471,-0.089279 -3.378213,-0.027091 \
-5.003929,-0.324922c0.046763,-1.228895 3.354712,-0.757366 3.50527,-1.916973c2.40519,-0.030278 \
0.133222,-2.53291 -0.84626,-3.129996c-1.253728,-1.007249 -2.088776,-2.597297 -1.135676,-3.841233 \
c1.363243,-0.630993 4.445648,-0.246902 4.306324,-1.90514c-0.92712,-0.368159 -2.104238,-0.134364 \
-3.143361,-0.199472c0.063904,-0.773238 0.127806,-1.546474 0.19171,-2.319711c1.374225,-0.073638 \
2.748453,-0.147281 4.122678,-0.220925c-0.697538,-0.505036 -1.741481,-0.876715 -2.002715,-1.591114 \
c0.888562,-1.017564 3.03006,-0.927739 4.564204,-0.918118c1.491783,-0.071682 4.211677,0.621513 \
2.178285,1.738058c-0.164809,0.274194 -1.76877,0.955962 -0.777544,0.881636c1.269146,0 2.538289,0 3.807432,0 \
c-0.003647,0.755909 -0.032817,1.519331 0.222013,2.259079c-0.968779,0.50261 -4.110267,-0.42933 \
-3.215785,1.121467c1.00718,0.648374 2.549715,0.631263 3.827187,0.916809c0.745128,0.05186 1.337877,0.187126 \
1.06936,0.793888c0.140053,1.12655 -0.267946,2.259795 -1.379505,3.114961c-0.78685,0.892343 -2.554104,2.009454 \
-2.030939,3.04235c0.989808,-0.112243 1.784926,0.34051 0.994348,0.849666c0.811317,0.418481 3.668275,0.322648 \
3.050353,1.370331c-1.44748,0.333793 -3.002438,0.194731 -4.504179,0.228793c0.399856,2.859806 0.952729,5.719883 \
1.876543,8.520375c0.539773,0.621425 2.683955,1.099034 0.92425,1.909172c-0.656721,1.508686 1.44088,2.638331 \
2.758263,3.606642c1.448451,0.755733 0.745692,2.276334 2.794824,2.518852c1.613781,1.089034 -2.215606,1.06004 \
-3.198757,1.062415c-6.508579,0.04889 -13.024179,0.11729 -19.530211,-0.028817c-0.532596,-0.046826 -1.190603,-0.036114 \
-1.57017,-0.35235l-0.000002,0.000001z"};
createSvgElem("path", svgAttr, svgStyle, svg);
}
initialise();
var div = document.getElementById("dvScale");
function initialise(){
let svgAttr = {"viewBox": "0 0 500 300"};
let svg = createSvgElem("svg", svgAttr, null, div);
// gradients are located in a "defs" element in the XML
let defs = createSvgElem("defs", null, null, svg);
// now we define the gradient attributes including an id
svgAttr = {x1: 0, x2: 0, y1: 0, y2: 1, id: "grad1"}; // vertical gradient
let grad = createSvgElem("linearGradient", svgAttr, null, defs);
// we now create 3 stops for the gradient
svgAttr = {offset: "0%", "stop-color": "red"};
createSvgElem("stop", svgAttr, null, grad);
svgAttr = {offset: "50%", "stop-color": "black", "stop-opacity": 0};
createSvgElem("stop", svgAttr, null, grad);
svgAttr = {offset: "100%", "stop-color": "blue"};
createSvgElem("stop", svgAttr, null, grad);
// now a rectangle to show the gradient off
svgAttr = {x: 20, y: 20, width: 100, height: 100, rx: 10, ry: 10};
createSvgElem("rect", svgAttr, {fill: "url(#grad1)"}, svg); // the style adds the gradient fill
}
initialise();
var div = document.getElementById("dvScale");
function initialise(){
let svgAttr = {"viewBox": "0 0 500 300"};
let svg = createSvgElem("svg", svgAttr, null, div);
// patterns like gradients are inside a defs element
let defs = createSvgElem("defs", null, null, svg);
// now a horizontal gradient
svgAttr = {id: "grad1"};
let grad = createSvgElem("linearGradient", svgAttr, null, defs);
// with 2 stops
svgAttr = {offset: "10%", "stop-color": "white"};
createSvgElem("stop", svgAttr, null, grad);
svgAttr = {offset: "95%", "stop-color": "blue"};
createSvgElem("stop", svgAttr, null, grad);
// now we define the pattern
svgAttr = {x: 0, y: 0, width: 0.25, height: 0.25, id: "patt1"};
let patt = createSvgElem("pattern", svgAttr, null, defs);
// we can now create the components of the pattern
svgAttr = {x: 0, y: 0, width: 50, height: 50};
createSvgElem("rect", svgAttr, {fill: "cyan"}, patt); // a square
svgAttr = {cx: 25, cy: 25, r: 20}; // with a circle inside it
createSvgElem("circle", svgAttr, {fill: "url(#grad1)", "fill-opacity": 0.5}, patt);
// and a rectangle to show the repeating pattern off
svgAttr = {x: 20, y: 20, width: 200, height: 200, rx: 10, ry: 10};
createSvgElem("rect", svgAttr, {fill: "url(#patt1)"}, svg);
}
initialise();
svgAttr = {x: 120, y: 0, width: 200, height: 200, rx: 10, ry: 10, transform: "rotate(30)"};
svgAttr = {x: 120, y: 0, width: 200, height: 200, transform: "rotate(30)"};
let rect = createSvgElem("rect", svgAttr, {fill: "url(#patt1)"}, svg);
svgAttr = {attributeName: "rx", values: "0;100;0", dur: "10s", repeatCount: "indefinite"};
createSvgElem("animate", svgAttr, null, rect);