Ribbon Animated Effect
How do I make an ribbon animated effect?
An animated ribbon effect based on svg path definition. What is a ribbon animated effect? How do you make a ribbon animated effect? This script and codes were developed by François Chazal on 21 September 2022, Wednesday.
Ribbon Animated Effect - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Ribbon Animated Effect</title> <script src="https://s.codepen.io/assets/libs/modernizr.js" type="text/javascript"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <style> /* NOTE: The styles were added inline because Prefixfree needs access to your styles and they must be inlined if they are on local disk! */ @import url(https://fonts.googleapis.com/css?family=Roboto+Condensed:300italic,400italic,700italic,400,300,700);
html { font-size: 11px;
}
body { overflow: hidden; min-height: 100%; margin: 0; padding: 0; font: 1em/1.5em 'Roboto Condensed', sans-serif;
}
/************************************/
#about { position: fixed; left: 0; bottom: 0; width: 100%; height: 50px; color: #fff; background: rgba(0, 0, 0, 0.8);
}
#about-title { position: absolute; left: 25px; bottom: 25px; font-size: 2em; font-weight: 700;
}
#about-author { position: absolute; left: 25px; top: 27px; font-size: .9em; font-weight: 300;
}
#about-author a { color: inherit; text-decoration: inherit;
}
/************************************/
#console { position: fixed; top: 0; right: 0; width: 200px; bottom: 50px; margin: 0; padding: 5px; background: rgba(0, 0, 0, 0.05); border-left: 2px solid rgba(0, 0, 0, 0.1); font-size: .9em; color: rgba(0, 0, 0, 0.3); overflow-y: auto;
}
#console::before { display: block; content: 'CONSOLE'; font-size: 1.1rem; font-weight: bold; margin-bottom: 5px;
}
/************************************/
#content { position: fixed; top: 0; left: 0; width: 100%; height: 100%;
}
#content .container { display: inline-block; margin-top: 20px; width: 256px; height: 256px; font-size: 1.1rem;
}
#content .container * { width: 100%; height: 100%;
}
#content .container::before { position: absolute; height: 20px; padding: 0px 10px; margin-top: -20px; line-height: 20px; color: #fff; font-weight: bold;
}
#content .container:nth-child(1) { background: rgba(128, 0, 0, 0.1);
}
#content .container:nth-child(1)::before { content: "PATH INPUT"; background: maroon;
}
#content .container:nth-child(2) { background: rgba(0, 128, 0, 0.1);
}
#content .container:nth-child(2)::before { content: "RIBBON RENDERING"; background: green;
}
#content .container:nth-child(3) { background: rgba(0, 0, 128, 0.1);
}
#content .container:nth-child(3)::before { content: "RIBBON OUTPUT"; background: navy;
} </style> <script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
</head>
<body> <section id="content"> <div class="container svg"> <svg id="input" viewBox="0 0 100 100"> <g style="fill: none; stroke-width: 0.1; stroke: #000"> <path d="M5,5 h42 v20 h8 l0.01,-18 v18 h8 v-20 h24 v8 h8 v12 h-24 v-12 h8 v6 h-8 v3"></path> <path d="M53,53 v-8 h-8 v8 h16 v-8 l-8,-8 h-8 l-8,8 v8 l8,8 h16"></path> </g> </svg> </div> <div class="container canvas"> <canvas id="canvas"></canvas> </div> <div class="container svg"> <svg id="output" viewBox="0 0 100 100"> </svg> </div> </section> <section id="about"> <div id="about-title">Ribbon Effect</div> <div id="about-author">an experimentation by François Chazal (<a href="twitter.com/fchazal">@fchazal</a>) for <a href="http://www.webtechs.fr">WebTechs</a></div> </section> <script src="js/index.js"></script>
</body>
</html>
Ribbon Animated Effect - Script Codes CSS Codes
@import url(https://fonts.googleapis.com/css?family=Roboto+Condensed:300italic,400italic,700italic,400,300,700);
html { font-size: 11px;
}
body { overflow: hidden; min-height: 100%; margin: 0; padding: 0; font: 1em/1.5em 'Roboto Condensed', sans-serif;
}
/************************************/
#about { position: fixed; left: 0; bottom: 0; width: 100%; height: 50px; color: #fff; background: rgba(0, 0, 0, 0.8);
}
#about-title { position: absolute; left: 25px; bottom: 25px; font-size: 2em; font-weight: 700;
}
#about-author { position: absolute; left: 25px; top: 27px; font-size: .9em; font-weight: 300;
}
#about-author a { color: inherit; text-decoration: inherit;
}
/************************************/
#console { position: fixed; top: 0; right: 0; width: 200px; bottom: 50px; margin: 0; padding: 5px; background: rgba(0, 0, 0, 0.05); border-left: 2px solid rgba(0, 0, 0, 0.1); font-size: .9em; color: rgba(0, 0, 0, 0.3); overflow-y: auto;
}
#console::before { display: block; content: 'CONSOLE'; font-size: 1.1rem; font-weight: bold; margin-bottom: 5px;
}
/************************************/
#content { position: fixed; top: 0; left: 0; width: 100%; height: 100%;
}
#content .container { display: inline-block; margin-top: 20px; width: 256px; height: 256px; font-size: 1.1rem;
}
#content .container * { width: 100%; height: 100%;
}
#content .container::before { position: absolute; height: 20px; padding: 0px 10px; margin-top: -20px; line-height: 20px; color: #fff; font-weight: bold;
}
#content .container:nth-child(1) { background: rgba(128, 0, 0, 0.1);
}
#content .container:nth-child(1)::before { content: "PATH INPUT"; background: maroon;
}
#content .container:nth-child(2) { background: rgba(0, 128, 0, 0.1);
}
#content .container:nth-child(2)::before { content: "RIBBON RENDERING"; background: green;
}
#content .container:nth-child(3) { background: rgba(0, 0, 128, 0.1);
}
#content .container:nth-child(3)::before { content: "RIBBON OUTPUT"; background: navy;
}
Ribbon Animated Effect - Script Codes JS Codes
/* BEGINNING OF LOADING BLOCK ****************/
var canvas = null;
var context = null;
var speed = 50; // px/s
var width = 1.5;
var darkColor = '#7A0000';
var lightColor = '#D30B00';
function start() { canvas = document.querySelector('#canvas'); context = canvas.getContext('2d'); canvas.width = canvas.offsetWidth; canvas.height = canvas.offsetHeight; context.save(); context.scale(canvas.width/100,canvas.height/100); parseSVG('#input'); context.restore();
}
window.addEventListener('load', function() { start();
});
/* END OF LOADING BLOCK **********************/
function det(v1, v2) { return v1[0]*v2[1] - v2[0]*v1[1];
}
function normalizeVector(vector) { var size = Math.sqrt(Math.pow(vector[0],2) + Math.pow(vector[1],2)); vector[0] /= size; vector[1] /= size; return vector;
}
function vectorFromSegment(segment) { return normalizeVector([segment[1][0] - segment[0][0], segment[1][1] - segment[0][1]]);
}
function segmentLength(segment) { return Math.sqrt(Math.pow(segment[1][0] - segment[0][0],2) + Math.pow(segment[1][1] - segment[0][1],2));
}
function parseSVG(id) { var steps = 10; var nodeList = document.querySelectorAll(id+' path'); for (var id=0; id < nodeList.length; id++) { var path = nodeList.item(id); var step = path.getTotalLength() / steps; var segList = path.pathSegList; var push = false; var segments = []; var segment = [[0,0], [0,0]]; for (var i = 0; i < segList.numberOfItems; i++) { seg = segList.getItem(i); //http://www.w3.org/TR/SVG/paths.html#InterfaceSVGPathSeg switch (seg.pathSegType) { case 2: //PATHSEG_MOVETO_ABS segment[0][0] = seg.x; segment[0][1] = seg.y; break; case 3: //PATHSEG_MOVETO_REL segment[0][0] += seg.x; segment[0][1] += seg.y; break; case 4: //PATHSEG_LINETO_ABS segment[1][0] = seg.x; segment[1][1] = seg.y; push = true; break; case 5: //PATHSEG_LINETO_REL segment[1][0] += seg.x; segment[1][1] += seg.y; push = true; break; case 12: //PATHSEG_LINETO_HORIZONTAL_ABS segment[1][0] = seg.x; push = true; break; case 13: //PATHSEG_LINETO_HORIZONTAL_REL segment[1][0] += seg.x; push = true; break; case 14: //PATHSEG_LINETO_VERTICAL_ABS segment[1][1] = seg.y; push = true; break; case 15: //PATHSEG_LINETO_VERTICAL_REL segment[1][1] += seg.y; push = true; break; default: } if (push) { segments.push([[segment[0][0],segment[0][1]],[segment[1][0],segment[1][1]]]); segment[0][0] = segment[1][0]; segment[0][1] = segment[1][1]; push = false; } else { segment[1][0] = segment[0][0]; segment[1][1] = segment[0][1]; } } var lengths = []; for (var i = 0; i < segments.length; i++) lengths.push(segmentLength(segments[i])); var vectors = []; for (var i = 0; i < segments.length; i++) vectors.push(vectorFromSegment(segments[i])); var points = []; for (var i = 0; i < segments.length; i++) points.push([segments[i][0][0], segments[i][0][1]]); points.push([segments[segments.length-1][1][0], segments[segments.length-1][1][1]]); var tangents = []; for (var i = 0; i < vectors.length-1; i++) tangents.push(normalizeVector([vectors[i][0]+vectors[i+1][0], vectors[i][1]+vectors[i+1][1]])); tangents.unshift([-vectors[0][1], vectors[0][0]]); tangents.push([-vectors[vectors.length-1][1], vectors[vectors.length-1][0]]); for (var i = 0; i < points.length-1; i++) { context.lineWidth=0.1; context.beginPath(); context.moveTo(points[i][0],points[i][1]); context.lineTo(points[i+1][0],points[i+1][1]); context.stroke(); context.closePath(); } var sections = []; for (var i = 0; i < points.length; i++) { if (!i) w = width else if (i == points.length-1) w = -width else w = width / det(tangents[i], vectors[i-1]); sections.push([ [points[i][0] + w * tangents[i][0], points[i][1] + w * tangents[i][1]], [points[i][0] - w * tangents[i][0], points[i][1] - w * tangents[i][1]] ]); } for (var i = 0; i < sections.length-1; i++) { context.lineWidth=0.2; context.beginPath(); context.moveTo(sections[i][0][0],sections[i][0][1]); context.lineTo(sections[i+1][1][0],sections[i+1][1][1]); context.lineTo(sections[i+1][0][0],sections[i+1][0][1]); context.lineTo(sections[i][1][0],sections[i][1][1]); context.lineTo(sections[i][0][0],sections[i][0][1]); context.fillStyle = (i%2) ? darkColor : lightColor; context.fill(); context.closePath(); } createSVG('#output', id, sections, lengths); }
}
var svgns = "http://www.w3.org/2000/svg";
function createSVG(id, node, sections, lengths) { var svgNode = document.querySelector(id); var defsNode = document.createElementNS(svgns, 'defs'); var groupNode = document.createElementNS(svgns, 'g'); for (var i = 0; i < sections.length-1; i++) { var d_init = 'M'+sections[i][0][0]+','+sections[i][0][1]+' '; d_init += 'L'+sections[i][1][0]+','+sections[i][1][1]+' '; d_init += 'L'+sections[i][1][0]+','+sections[i][1][1]+' '; d_init += 'L'+sections[i][0][0]+','+sections[i][0][1]+' '; d_init += 'L'+sections[i][0][0]+','+sections[i][0][1]; var d_final = 'M'+sections[i][0][0]+','+sections[i][0][1]+' '; d_final += 'L'+sections[i][1][0]+','+sections[i][1][1]+' '; d_final += 'L'+sections[i+1][0][0]+','+sections[i+1][0][1]+' '; d_final += 'L'+sections[i+1][1][0]+','+sections[i+1][1][1]+' '; d_final += 'L'+sections[i][0][0]+','+sections[i][0][1]; var animNode = document.createElementNS(svgns, 'animate'); animNode.setAttributeNS(null, 'id', 'anim_'+node+'_'+i); animNode.setAttributeNS(null, 'attributeName', 'd'); animNode.setAttributeNS(null, 'from', d_init); animNode.setAttributeNS(null, 'to', d_final); animNode.setAttributeNS(null, 'dur', (lengths[i]/speed)+'s'); animNode.setAttributeNS(null, 'fill', 'freeze'); if (i) animNode.setAttributeNS(null, 'begin', 'anim_'+node+'_'+(i-1)+'.end'); var pathNode = document.createElementNS(svgns, 'path');
// pathNode.setAttributeNS(null, 'd', d_final); pathNode.appendChild(animNode); var clipNode = document.createElementNS(svgns, 'clipPath'); clipNode.setAttributeNS(null, 'id', 'clip_'+node+'_'+i); clipNode.appendChild(pathNode); defsNode.appendChild(clipNode); var pathNode = document.createElementNS(svgns, 'path'); pathNode.setAttributeNS(null, 'd', d_final); pathNode.setAttributeNS(null, 'clip-path', 'url(#clip_'+node+'_'+i+')'); pathNode.setAttributeNS(null, 'fill', (i%2) ? darkColor : lightColor); groupNode.appendChild(pathNode); } svgNode.appendChild(defsNode); svgNode.appendChild(groupNode);
}
Developer | François Chazal |
Username | fchazal |
Uploaded | September 21, 2022 |
Rating | 3 |
Size | 5,909 Kb |
Views | 20,240 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
World | 5,903 Kb |
HExa | 2,185 Kb |
Obama | 223,882 Kb |
Folding Panel | 6,671 Kb |
WebTechs Animated Logo | 2,503 Kb |
Parallax Scrolling | 6,472 Kb |
Hexagona | 3,420 Kb |
Penrose Triangle CSS | 3,283 Kb |
Logo | 3,497 Kb |
The Cube | 6,949 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Pomodoro Timer | Sdas13 | 2,900 Kb |
Out of the blue | Giaco | 2,537 Kb |
Rrremark.com Overlay Highlighter | Derickruiz | 4,438 Kb |
RequestAnimationFrame | Martin-banks | 2,541 Kb |
Starfield using KineticJS | Asp | 3,512 Kb |
CSS Link Icons with jQuery Titles | Nicwinn | 2,312 Kb |
Mondrian | Chrisarmstrong | 2,787 Kb |
A Pen by Final Boss tommyb9 | FBtommyb9 | 55,354 Kb |
Cut and Paste Roll Link | BottomlineInteractive | 2,546 Kb |
Email Marketing Mock | Kristenzirkler | 8,224 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!