Vincent Tang
Fullstack Developer and cohost of CodeChefs
Fundamentals
Showcase Examples
Web makes communication standards easier
The ones that do it best have
great UX design and Animations
mastery requires knowledge in:
SVG, Canvas, WebGL (and CSS)
Software solves problems
...opens oppurtunities for bigger, meaningful projects (dev/agency).
SVG is an XML based vector image format for 2-dimension graphics with support for interactivity and animation. SVG specification was developed by W3C in 1999
- Wikipedia
Can be rendered in any size and resolution
Comprised of vectors drawn with strokes and fill
graphics
Pros:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
x="0px" y="0px" width="450px" height="100px" viewBox="0 0 450 100">
<rect x="10" y="5" fill="white" stroke="black" width="90" height="90"/>
<circle fill="white" stroke="black" cx="170" cy="50" r="45"/>
<polygon fill="white" stroke="black" points="279,5 294,35 328,40 303,62 309,94
279,79 248,94 254,62 230,39 263,35 "/>
<line fill="none" stroke="black" x1="410" y1="95" x2="440" y2="6"/>
<line fill="none" stroke="black" x1="360" y1="6" x2="360" y2="95"/>
</svg>
// Returns the bezier points for a smile. Scale is 0-1.
function smilePoints(scale) {
const factor = scale * 2 - 1; // i.e sad = -1, happy = +1
const p1 = { x: -20, y: -10 * factor };
const c1 = { x: -20, y: 10 * factor };
const p2 = { x: 20, y: -10 * factor };
const c2 = { x: 20, y: 10 * factor };
return [p1, c1, c2, p2];
}
function writeSmilePoints(points) {
console.log(points);
const p = p => `${p.x},${p.y}`; // write a point as a coord
return `M${p(points[0])} C${p(points[1])} ${p(points[2])} ${p(
points[3]
)}`;
}
function scaleSmile(scale) {
// Get the geometry of the new smile.
const points = writeSmilePoints(smilePoints(scale));
const svg = document.getElementById("svg");
// Create the geometry animation, which will look summat like this:
// <animate attributeName="d" attributeType="XML"
// to="M-20,10 C-20,-10 20,-10 20,10" dur="3s" repeatCount="indefinite"
// fill="freeze" />
const smilePath = document.getElementById("smilepath");
const animate = document.createElementNS(svg.namespaceURI, "animate");
animate.setAttribute("attributeName", "d");
animate.setAttribute("attributeType", "XML");
animate.setAttribute("to", points);
animate.setAttribute("dur", "0.3s");
animate.setAttribute("repeatCount", "1");
animate.setAttribute("fill", "freeze");
smilePath.appendChild(animate);
animate.beginElement();
}
function onChange() {
// Get the slider value, scale it from percentage to 0-1.
const val = this.value;
const scale = parseInt(val, 10) / 100;
scaleSmile(scale);
}
<svg id="svg" viewbox="0 0 120 120">
<g id="face" transform="translate(60 60)">
<!-- Yellow Background-->
<circle
id="facecircle"
cx="0"
cy="0"
r="50"
stroke="#000000"
stroke-width="2"
fill="#FAD257"
/>
<!-- Left Eye, Right Eye-->
<circle cx="-20" cy="-10" r="5" fill="#000000" />
<circle cx="20" cy="-10" r="5" fill="#000000" />
<!-- Smile -->
<g id="smile" transform="translate(0, 25)">
<path
id="smilepath"
fill="none"
stroke="#000000"
stroke-width="3"
stroke-linecap="round"
d="M-20,-10 C-20,10 20,10 20,-10"
/>
</g>
</g>
</svg>
Hand Code
From Adobe Illustrator, Sketch, etc
SMIL
tanuki-nose {
fill: #e24329;
transition: all 0.8s;
}
.tanuki-nose:hover {
fill: #f1a699;
transition: all 0.1s;
}
svg {
animation: grow 5s infinite;
}
@keyframes grow {
50% {
transform: scale(1.5);
}
}
1. Starting State
2. Ending State
3. Use a library
- Adobe AfterEffects + Plugin (Lottie, BodyMoving)
SVG tool for one-time CSS Animations
6 kB
~200 kB minified per graphic
~32 kB minified
One-time CSS Keyframe
300kB to 10mb
Can set loop amount
pen by sarah drasner
SVG Assets to download
https://www.flaticon.com/
52 kB
SVG Resources
Some Common Libraries
Tooling
Real World SVG Examples
CSS
Addional Resources Comparing Technologies
More Real World SVG
Examples that don't need webGL, SVG, or Canvas
Splitting.js
Anime.js 3.0 (really nice docs)
SVGs
Native WebGL2
Canvas
WebGL
Shader + lighting support
3D + camera
Three.js
Pixi.js
Canvas
OTHER
SVG
It depends.
check for edge cases first.
By Vincent Tang
Things you should know about SVG as a Developer and/or Designer