I have to apply some styles on <img>
thanks to a CSS class.
Is it possible to get the dataURL
of the <img>
with the CSS style ?
$(function() {
// Original
const imgOriginal = document.getElementById('original');
const c1 = document.getElementById('c1');
let ctx = c1.getContext('2d');
ctx.drawImage(imgOriginal, 100, 100);
// Filtered
const imgFiltered = document.getElementById('filtered');
const c2 = document.getElementById('c2');
ctx = c2.getContext('2d');
ctx.drawImage(imgFiltered, 100, 100);
// Same dataURL :(
console.log(c1.toDataURL(), c2.toDataURL());
console.log(c1.toDataURL() === c2.toDataURL());
})
.filter::before {
display: block;
height: 100%;
left: 0;
position: absolute;
top: 0;
width: 100%;
z-index: 1;
border: 1px solid red;
}
.filter {
position: relative;
-webkit-filter: sepia(.5) hue-rotate(-30deg) saturate(1.4);
filter: sepia(.5) hue-rotate(-30deg) saturate(1.4);
}
canvas {
display: block;
width: 100px;
height: 100px;
border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<img id="original" src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/c7/Brad_Pitt_Inglorious_Basterds_Berlin_premiere.jpg/170px-Brad_Pitt_Inglorious_Basterds_Berlin_premiere.jpg">
<canvas id="c1"></canvas>
<img id="filtered" class="filter" src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/c7/Brad_Pitt_Inglorious_Basterds_Berlin_premiere.jpg/170px-Brad_Pitt_Inglorious_Basterds_Berlin_premiere.jpg">
<canvas id="c2"></canvas>
</div>
Maybe snippet is going to have a bug because of the <canvas>
tag, the idea is there anyway.
EDIT :
If anyone has a suggestion with SVG
or something else, I'm using fabricJS.
EDIT 2 (NOT RESOLVE BUT FIND OTHER WAY) :
Thanks to @KavianK. you could replicate
CSS
style with thecanvas
context, however to me it's boring because we have to store a differentcallback
for eachCSS
class to get thedataURL
. Working anyway!Thanks to @Emeeus maybe a solution provide from your backend, not solution for me beacause i'm want to do this ONLY on the front-end. wkhtmltopdf
Thanks to @pegasuspect we can filter an image with
SVG
, I'm following this way and I replace fabricJS by svgjs, this librairie can replace easly acanvas
and it's more easier to work withimg
and I dind't need theDataURL
anymore !Thanks to @Kaiido there is a way to take a
snapshot
of yourHTML
rendered withCSS
style with html2canvas easy to getdataURL
with this case. Unfortunataly someCSS
styles are not supported yet likebox-shadow
orfilter
that's why it's not a solution for me
This topic is not resolve but with svgjs
I don't need actually work with dataURL
.