Skip to content

Instantly share code, notes, and snippets.

@tripolskypetr
Last active May 25, 2020 15:13
Show Gist options
  • Select an option

  • Save tripolskypetr/d6cc67356c8109e6c784cf75b6fc968e to your computer and use it in GitHub Desktop.

Select an option

Save tripolskypetr/d6cc67356c8109e6c784cf75b6fc968e to your computer and use it in GitHub Desktop.
Transparent image https://jsfiddle.net/7haLkg6s/
<div style="display: flex;">
<div style="background: yellow;">
<img>
</div>
</div>
<script>
(function() {
const transparentImage = (
h = 100,
w = 100,
rgba = {r: 255, g: 255, b: 255, a: 0}
) => {
const canvas = document.createElement('canvas');
const {r, g, b, a} = rgba;
const ctx = canvas.getContext('2d');
canvas.width = w;
canvas.height = h;
ctx.fillStyle = 'rgba(' + [r,g,b,a].join() + ')';
ctx.fillRect(0,0,w,h);
return canvas.toDataURL('image/png','');
};
document.querySelector('img').src = transparentImage();
})();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment