Skip to content

Instantly share code, notes, and snippets.

@royling
Last active February 25, 2016 02:03
Show Gist options
  • Save royling/a758ba977c4cbf3751d9 to your computer and use it in GitHub Desktop.
Save royling/a758ba977c4cbf3751d9 to your computer and use it in GitHub Desktop.
CSS blending
<!DOCTYPE html>
<head>
<title>CSS Blending</title>
<link rel="stylesheet" href="main.css">
</head>
<body>
<div>
<input id="backdropColor" type="text" placeholder="Backdrop color, eg. #ffffff/white"><br>
<input id="sourceColor" type="text" placeholder="Source color, eg. #000000/black"><br>
<select name="blendingMode" id="blendingMode"></select>
</div>
<svg class="canvas">
<circle id="backdrop" cx="120" cy="120" r="120"/>
<circle id="source" cx="240" cy="120" r="120"/>
</svg>
<script src="main.js"></script>
</body>
</html>
.canvas {
width: 360px;
height: 240px;
margin: 30px auto;
}
circle {
transition: fill 2s;
}
'use strict';
const backdrop = document.querySelector('#backdrop'),
source = document.querySelector('#source'),
backdropColorInput = document.querySelector('#backdropColor'),
sourceColorInput = document.querySelector('#sourceColor'),
modeSelector = document.querySelector('#blendingMode');
const blendingModes = {
"Separable": [
"normal",
"multiply",
"screen",
"overlay",
"darken",
"lighten",
"color-dodge",
"color-burn",
"hard-light",
"soft-light",
"difference",
"exclusion"
],
"Non-separable": [
"hue",
"saturation",
"color",
"luminosity"
]
};
const initModeOptions = () => {
modeSelector.innerHTML = Object.keys(blendingModes).map(group => {
let options = blendingModes[group].map(mode => `<option value="${mode}">${mode}</option>`).join('');
return `<optgroup label="${group}">${options}</optgroup>`;
}).join('');
// select first option
modeSelector.querySelector('option').setAttribute('selected', 'selected');
};
initModeOptions();
const showEffect = () => {
backdrop.setAttribute('fill', backdropColorInput.value || '#f00');
source.setAttribute('fill', sourceColorInput.value || '#00f');
backdrop.style.mixBlendMode = source.style.mixBlendMode = modeSelector.value || 'normal';
};
[backdropColorInput, sourceColorInput, modeSelector].forEach(elem => {
elem.addEventListener('change', showEffect, false);
});
showEffect(); // init display
@royling
Copy link
Author

royling commented Jan 31, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment