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 includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

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

              
                <div id="wrap">
<svg id="maskDemo" xmlns="http://www.w3.org/2000/svg" width="400" height="200" viewBox="0 0 400 200">
  <title>Invert SVG text fill color with masks</title>
  <defs>
	<mask id="theMask">
	  <rect id="maskerH" width="400" height="200" x="-400" y="0" fill="#fff"/>
	  <rect id="maskerV" width="400" height="200" x="0" y="200" fill="#fff"/>
	</mask>
  </defs>
  
	<!-- this is the fixed color background with white as the fill, but the stroke changes -->
  <rect id="bgFixed" width="400" height="200" fill="white" stroke="#94c356" stroke-width="4"/>

	<!-- this text color changes based on the chosen color swatch-->
  <g id="startColor" fill="#94c356" font-size="100">
	  <text class="theCount" text-anchor="end" x="220" y="140" >100</text>
	  <text text-anchor="start" x="230" y="140" >%</text>
  </g>

  <g mask="url(#theMask)">
	  <!-- this is the changeable color background based on the swatch click-->
		<rect id="bgChange" width="400" height="200" fill="#94c356"/>
	  
	  <!-- this is the duplicate text group revealed by the masks it's always white -->
		<g id="end" fill="white" font-size="100">
			<text class="theCount" text-anchor="end" x="220" y="140" >100</text>
			<text text-anchor="start" x="230" y="140" >%</text>
		</g>
    </g>	
</svg>

<div id="colorControls">
    <div class="swatch"></div>
    <div class="swatch"></div>
    <div class="swatch"></div>
    <div class="swatch"></div>
    <div class="swatch"></div>
    <div class="swatch"></div>
    <div class="swatch"></div>
    <div class="swatch"></div>
</div>

<button id="toggleDirection">Switch To Vertical</button>

</div>
<img class="avatar" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/314556/demoAvatar.png" alt="Craigs avatar">
<a class="twitterLink" href="https://twitter.com/Craig_PointC" target="_top">Follow me on Twitter</a>
              
            
!

CSS

              
                html {
  font-size: 20px;
}

body {
  font-family: "Source Sans Pro", sans-serif;
  padding: 0;
  margin: 0;
  overflow: hidden;
  width: 100%;
  height: 100vh;
  background: #e0e0e0;
}

#wrap {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  opacity: 0;
}

#maskDemo {
  font-weight: 700;
  margin-bottom: 12px;
}

#colorControls {
  display: flex;
  margin-bottom: 1rem;
}

.swatch {
  width: 46px;
  height: 46px;
  margin: 0 2px;
  cursor: pointer;
  border-radius: 4px;
}

#toggleDirection {
  width: 300px;
  height: 60px;
  cursor: pointer;
  color: #676767;
  font-weight: 600;
  font-size: 1.2rem;
    font-family: "Source Sans Pro", sans-serif;
}
.avatar {
  position: absolute;
  bottom: 0;
  right: 0;
  display: block;
  width: 140px;
  height: auto;
  z-index: 10;
}

.twitterLink {
  position: absolute;
  text-decoration: none;
  font-size: 0.6rem;
  color: white;
  background: #42a6e0;
  padding: 4px 12px;
  border-radius: 4px;
  bottom: 30px;
  right: 70px;
  z-index: 20;
}

              
            
!

JS

              
                console.clear();
// timelines - 1 for vertical and 1 for horizontal
const tlh = gsap.timeline({
  yoyo: true,
  repeat: -1,
  repeatDelay: 0.4,
  onUpdate: changeIt
});
const tlv = gsap.timeline({
  yoyo: true,
  repeat: -1,
  repeatDelay: 0.4,
  paused: true,
  onUpdate: changeIt
});
const	colorArray = ["#94c356", "#46a4cc", "#50a39e", "#a63e4b", "#e3aa59", "#a63ba0", "#a2a2a2", "#0f0f0f"];
let count = document.querySelectorAll(".theCount");
let swatches = document.querySelectorAll(".swatch");

// prevent FOUC
gsap.set("#wrap", { autoAlpha: 1 });

// fill the 8 swatches from the color array and listen for click
swatches.forEach((obj, i) => {
  gsap.set(obj, { backgroundColor: colorArray[i] });
  obj.index = i;
  obj.addEventListener("click", colorChange);
});

// alternating horizontal and vertical timeline animations
tlh.to("#maskerH", { duration: 4, attr: { x: 0 }, ease: "none" });
tlv.to("#maskerV", { duration: 2, attr: { y: 0 }, ease: "none" });

// change the percentage counter based on the active timeline progress
function changeIt() {
  newPercent = (this.progress() * 100).toFixed();
  count.forEach((obj, i) => {
    obj.textContent = newPercent;
  });
}

//change colors on click
function colorChange() {
  gsap.to("#startColor, #bgChange", {
    fill: colorArray[this.index],
    ease: "none"
  });
  gsap.to("#bgFixed", { stroke: colorArray[this.index], ease: "none" });
}

// toggle listener and timeline switcher
document.querySelector("#toggleDirection").addEventListener("click", toggleTimelines);

function toggleTimelines() {
  if (tlh.isActive()) {
    this.textContent = "Switch To Horizontal";
    tlh.pause(0);
    tlv.play();
  } else {
    this.textContent = "Switch To Vertical";
    tlv.pause(0);
    tlh.play();
  }
}

              
            
!
999px

Console