I have an object that has a method that creates a img
element within a canvas, the problem is that I could not give it " style ", I was working with a div
in instead of a canvas and had no problems. I load the image well, just without style, I tried with: setAttributte('style','...')
and with oImg.style['atributo'].
Can someone tell me what I'm doing wrong?
Thanks in advance:
Code:
<body style="background-color: black;">
<canvas id="myCanvas" width="600" height="400"
style="border:1px solid #c3c3c3;">
</canvas>
<script>
let objeto = new Personaje("id23","30%",0);
objeto.nacimiento();
</script>
</body>
And here is the method of my class:
nacimiento(){
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var oImg = document.createElement('img');
oImg.src = "transparente.png";
oImg.style.width="22px";
oImg.style.height="22px";
oImg.style.background="url('sprite.png') -3px -3px";
oImg.style.position="absolute";
oImg.onload = function(){
ctx.drawImage(oImg, 0, 0);
}
}