Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <link href='//fonts.googleapis.com/css?family=Signika+Negative:300,400,600' rel='stylesheet' type='text/css'>
<svg viewBox="0 34 318 318" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMin">
	<text x="160" y="58"  text-anchor="middle" alignment-baseline="central">Fixing origin weirdness</text>
	<polygon class="bird" fill="#D8D8D8" points="77.2304688 91.9453125 159 106.492188 184 138.167969 226.898438 115.056641 253.035156 129.8125 226.898438 129.8125 209.027344 167 146.144531 198.804688 123.304688 240.269531 130.894531 167 69.1875 122.101562 91.6328125 115.056641"></polygon>
	<path d="M175.676694,154.30152 L191.511695,155.82645 L210.962219,151.025394 L233.717151,155.82645 L242.95394,154.30152 C246.77832,152.244275 247.61217,151.123744 242.95394,146.194876 L230.087487,136.938053 L228.310365,127.939313 L217.984677,116.68971 L200.922897,107.825315 L172.061171,100.416322 L149.036371,93.2771959 L137.366057,93.2771959 L123.866769,96.9103958 L118.780525,101.045621 L136.032037,107.825315 L121.971801,121.095982 L107.78547,136.938053 L100.10543,164.102792 L101.795345,197.402607 L103.508829,211.042132 L94.1860125,234.383939 L94.1860125,252.182258 C94.9577165,256.086947 96.3827429,257.522276 100.10543,250.273149 L105.796225,238.164446 L118.300891,231.264549 L121.971801,226.980837 L133.751712,221.52927 L145.260577,219.924204 C147.809753,220.110729 149.728122,219.756979 147.910939,216.203798 L133.751712,205.522214 L121.971801,204.083311 L118.300891,199.915089 L121.971801,181.674847 L131.419535,162.505975 L147.047126,155.82645 L149.036371,162.505975 L145.260577,176.350553 L144.090364,181.744376 L147.047126,181.674847 L159,176.350553 L168.569976,167 L175.676694,154.30152 Z" class="dolphin" stroke="#979797" fill="#D33838"></path>
</svg>
<div id="instructions">
  <p>In the JS panel, uncomment the <code>findMorphOrigin()</code> helper function and you'll get a GUI that'll let you drag the start and/or end origin to new positions and see how it affects the morph. Feel free to copy the findMorphOrigin() function into your own project, and then simply feed in the start and end shapes like <code>findMorphOrigin( ".startSelector", ".endSelector" );</code>. Then use the resulting origin value for your tween(s).</p></div>
              
            
!

CSS

              
                body{
  background: #222;
  text-align: center;
  font-family: "Signika Negative", sans-serif; 
  font-size: 20px;
  font-weight: 300;
}

svg {
  position: fixed;
  height: 100vh;
  width: 100vw;
  top: 0;
  left: 0;
}

.dolphin {
  visibility: hidden;
}

.bird {
  fill: #765e98;
}

text {
  fill: white;
  font-size: 20px;
}

#instructions {
  position: fixed;
  bottom: 0;
  left: 0;
  right:0;
  text-align: center;
}
p {
  color: #ccc;
  padding: 20px;
  max-width: 800px;
  margin-bottom: 0;
  text-align: left;
  display: inline-block;
}
code {
  color: white;
  font-family: courier, monospace;
}
              
            
!

JS

              
                gsap.registerPlugin(MorphSVGPlugin, Draggable);
gsap.defaults({overwrite: "auto"});

MorphSVGPlugin.convertToPath(".bird");

// var tween = gsap.to(".bird", {
//   duration: 2, 
//   morphSVG:{
//     shape: ".dolphin", 
//     //origin: "42% 57%,38% 19%",
//     type: "rotational"
//   }, 
//   ease: "power1.inOut",
//   repeat: -1,
//   repeatDelay: 1,
//   yoyo: true,
//   delay: 3
// });

findMorphOrigin(".bird", ".dolphin", {showLines:true});


/*
This function helps visualize the morph's origin (only for type:"rotational" morphs).
Simply pass in the the two targets (the starting SVG element and ending SVG element)
and it'll morph them, adding a red circle to show the origin. You can drag it around and once
you release it, you'll see the animation updated accordingly. If you drag it when the shape
is in its "end" state, it'll update the end origin. Drag in its "start" state and it'll
update the start origin.

Example:
findMorphOrigin(".start", ".end", {showLines:true});

Optionally pass in a vars object as the 3rd parameter with things like:
{showLines:true, scale:1.5, lineColor:"white", lineOpacity:0.5, lineWidth:1, origin:"40% 20%"}

Draggable is required (you can load it from the CDN at https://cdnjs.cloudflare.com/ajax/libs/gsap/2.1.0/utils/Draggable.min.js)
*/
function findMorphOrigin(target1, target2, vars) {
	vars = vars || {};
	gsap.killTweensOf(target1, false, {morphSVG:true});
	var _getElement = function(e) {
			return (typeof(e) === "string") ? document.querySelector(e) : e;
		},
		_setDefaults = function(v, defaults) {
			v = v || {};
			for (var p in defaults) {
				if (!(p in v)) {
					v[p] = defaults[p];
				}
			}
			return v;
		},
		_createSVG = function(type, container, attributes) {
			var element = document.createElementNS("http://www.w3.org/2000/svg", type),
				reg = /([a-z])([A-Z])/g,
				p;
			for (p in attributes) {
				element.setAttributeNS(null, p.replace(reg, "$1-$2").toLowerCase(), attributes[p]);
			}
			container.appendChild(element);
			return element;
		},
		editing = "start",
		lines, startOrigin, endOrigin,
		_createLines = function() {
			var rawPath = startElement._gsRawPath,
				i, j, segment, line;
			lines = [];
			for (j = 0; j < rawPath.length; j++) {
				segment = rawPath[j];
				for (i = 0; i < segment.length; i+=6) {
					line = _createSVG("line", startElement.ownerSVGElement, {stroke:v.lineColor, strokeWidth:v.lineWidth});
					line.style.opacity = v.lineOpacity;
					lines.push(line);
				}
				g.parentNode.appendChild(g);
			}
			return lines;
		},
		_update = function(leaveOrigin) {
			var rawPath = startElement._gsRawPath,
				li = 0,
				localToGlobal = globalG.getCTM().inverse().multiply(startElement.getCTM()),
				globalToLocal = localToGlobal.inverse(),
				o, j, i, sl, line, segment, ox, oy, x, y;
			if (rawPath && rawPath.origin) {
				if (leaveOrigin === true) {
					ox = gsap.getProperty(g, "x");
					oy = gsap.getProperty(g, "y");
				} else {
					o = rawPath.origin;
					ox = o.x * localToGlobal.a + o.y * localToGlobal.c + localToGlobal.e;
					oy = o.x * localToGlobal.b + o.y * localToGlobal.d + localToGlobal.f;
					gsap.set(g, {x:ox, y:oy});
				}
				if (v.showLines) {
					if (!lines) {
						_createLines();
					}
					for (j = 0; j < rawPath.length; j++) {
						segment = rawPath[j];
						sl = segment.length;
						for (i = 0; i < sl; i+=6) {
							line = lines[li++];
							line.setAttribute("x1", ox);
							line.setAttribute("y1", oy);
							line.setAttribute("x2", segment[i] * localToGlobal.a + segment[i+1] * localToGlobal.c + localToGlobal.e);
							line.setAttribute("y2", segment[i] * localToGlobal.b + segment[i+1] * localToGlobal.d + localToGlobal.f);
						}
					}
				}
				if (leaveOrigin === true) {
					x = ((ox * globalToLocal.a + oy * globalToLocal.c + globalToLocal.e) - rawPath.left) / rawPath.width;
					y = ((ox * globalToLocal.c + oy * globalToLocal.d + globalToLocal.f) - rawPath.top) / rawPath.height;
					if (editing === "start") {
						startOrigin = Math.round(x * 100) + "% " + Math.round(y * 100) + "%";
					} else {
						endOrigin = Math.round(x * 100) + "% " + Math.round(y * 100) + "%";
					}
					label.textContent = startOrigin + (endOrigin ? ", " + endOrigin : "");
				}
			}
		},
		startElement = _getElement(target1),
		v = _setDefaults(vars, {fill:"green", scale:1, origin:"50% 50%", lineColor:"white", lineWidth:0.5, lineOpacity:0.35, duration:2.25, ease:"power1.inOut", draggable:true}),
		globalG = _createSVG("g", startElement.ownerSVGElement), //Firefox returns null for ownerSVGElement.getCTM(), so we need a dummy element
		labelG = _createSVG("g", startElement.ownerSVGElement),
		rect = _createSVG("rect", labelG, {width:120, height:17}),
		label = _createSVG("text", labelG, {textAnchor:"middle"}),
		g = _createSVG("g", startElement.ownerSVGElement),
		clickArea = _createSVG("circle", g, {r:20, style:"fill:transparent"}),
		origin = _createSVG("circle", g, {r:4, style:"fill:" + v.fill + ";stroke:" + v.lineColor}),
		tween = gsap.to(target1, {duration: v.duration, morphSVG:{shape:target2, type:"rotational", origin:v.origin, smoothTolerance:v.smoothTolerance}, onUpdate:_update, ease:v.ease}),
		bbox = startElement.getBBox(),
		globalToLocal = globalG.getCTM().inverse().multiply(startElement.getCTM()).inverse(),
		localToGlobal = globalG.getCTM().inverse().multiply(startElement.getCTM()),
		tl = gsap.timeline({yoyo:true, repeat:-1, repeatDelay:1.25}),
		rawPath;

	gsap.set(rect, {y:-17, x: -60, fill:"black", opacity:0.4});
	gsap.set(label, {y:-5, fontSize:10, fill:"white"});
	gsap.set([g, labelG], {scale:v.scale, svgOrigin:"0 0"});
	gsap.set(labelG, {x:bbox.x + bbox.width / 2 + localToGlobal.e, y:bbox.y + bbox.height + localToGlobal.f});
	tl.add(tween, 0)
	  .to(origin, {duration: v.duration, fill:"red", ease:"steps(1)"}, 0);
	tl.time(0.0001);
	rawPath = startElement._gsRawPath;
	if (!rawPath || !rawPath.origin) {
		throw "Please update to the latest MorphSVGPlugin";
	}
	if (v.origin.indexOf(",") !== -1) {
		endOrigin = v.origin.split(",")[1];
	}
	_update();
	_update(true);
	if (typeof(Draggable) !== "undefined") {
		Draggable.create(g, {
			onPress:function() {
				if (tl.pause().progress() < 0.5) {
					editing = "start";
					tl.progress(0.0001);

				} else {
					editing = "end";
					tl.progress(0.99999);
				}
				MorphSVGPlugin.getTotalSize(startElement._gsRawPath); //re-calculates the top/left/width/height and attaches those to the rawPath.
				gsap.set(g, {x:this.x, y:this.y});
				_update(true);
			},
			onDrag:function() {
				_update(true);
			},
			onRelease:function() {
				var time = tl.time(); //globalToLocal = startElement.ownerSVGElement.getCTM().inverse().multiply(startElement.getCTM()).inverse()
				tween.vars.morphSVG.origin = startOrigin + (endOrigin ? "," + endOrigin : "");
				tl.totalTime(0).invalidate().play(time > 0.001 ? time + 1.25 : 0);
			}
		});
	} else {
		console.log("Please load Draggable for findMorphOrigin() to work.")
	}
	return tl;
}
              
            
!
999px

Console