How can I replace ctx.fillStyle or ctx.fillRect with an image? [duplicate]










0















This question already has an answer here:



  • How would I fillRect with an image?

    3 answers



I've not been coding for far too long, however, I'm trying to make a simple "space" invaders style game and when I made the placeholders I didn't think about how to implement my own custom images. Is there a way to substitute ctx.fillStyle or ctx.fillRect for an image? Or is there an easier better way? My image will be needed to move left and right. Here an example of what I have now:



//Enemies
function enemy (x,y,w,h)
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.speed = 50;
this.show = function()
ctx.fillStyle = "green";
ctx.fillRect(this.x,this.y,this.w,this.h);

this.move = function(speed)
this.clear();
this.y += speed;
this.show();

this.clear = function()
ctx.clearRect(this.x,this.y,this.w,this.h);











share|improve this question















marked as duplicate by Kaiido javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 12 '18 at 0:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • developer.mozilla.org/en-US/docs/Web/API/…
    – Chris G
    Nov 11 '18 at 23:34















0















This question already has an answer here:



  • How would I fillRect with an image?

    3 answers



I've not been coding for far too long, however, I'm trying to make a simple "space" invaders style game and when I made the placeholders I didn't think about how to implement my own custom images. Is there a way to substitute ctx.fillStyle or ctx.fillRect for an image? Or is there an easier better way? My image will be needed to move left and right. Here an example of what I have now:



//Enemies
function enemy (x,y,w,h)
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.speed = 50;
this.show = function()
ctx.fillStyle = "green";
ctx.fillRect(this.x,this.y,this.w,this.h);

this.move = function(speed)
this.clear();
this.y += speed;
this.show();

this.clear = function()
ctx.clearRect(this.x,this.y,this.w,this.h);











share|improve this question















marked as duplicate by Kaiido javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 12 '18 at 0:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • developer.mozilla.org/en-US/docs/Web/API/…
    – Chris G
    Nov 11 '18 at 23:34













0












0








0








This question already has an answer here:



  • How would I fillRect with an image?

    3 answers



I've not been coding for far too long, however, I'm trying to make a simple "space" invaders style game and when I made the placeholders I didn't think about how to implement my own custom images. Is there a way to substitute ctx.fillStyle or ctx.fillRect for an image? Or is there an easier better way? My image will be needed to move left and right. Here an example of what I have now:



//Enemies
function enemy (x,y,w,h)
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.speed = 50;
this.show = function()
ctx.fillStyle = "green";
ctx.fillRect(this.x,this.y,this.w,this.h);

this.move = function(speed)
this.clear();
this.y += speed;
this.show();

this.clear = function()
ctx.clearRect(this.x,this.y,this.w,this.h);











share|improve this question
















This question already has an answer here:



  • How would I fillRect with an image?

    3 answers



I've not been coding for far too long, however, I'm trying to make a simple "space" invaders style game and when I made the placeholders I didn't think about how to implement my own custom images. Is there a way to substitute ctx.fillStyle or ctx.fillRect for an image? Or is there an easier better way? My image will be needed to move left and right. Here an example of what I have now:



//Enemies
function enemy (x,y,w,h)
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.speed = 50;
this.show = function()
ctx.fillStyle = "green";
ctx.fillRect(this.x,this.y,this.w,this.h);

this.move = function(speed)
this.clear();
this.y += speed;
this.show();

this.clear = function()
ctx.clearRect(this.x,this.y,this.w,this.h);






This question already has an answer here:



  • How would I fillRect with an image?

    3 answers







javascript image this






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 0:12









Liam Strilchuk

12915




12915










asked Nov 11 '18 at 23:32









Travis Smith

61




61




marked as duplicate by Kaiido javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 12 '18 at 0:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Kaiido javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 12 '18 at 0:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • developer.mozilla.org/en-US/docs/Web/API/…
    – Chris G
    Nov 11 '18 at 23:34
















  • developer.mozilla.org/en-US/docs/Web/API/…
    – Chris G
    Nov 11 '18 at 23:34















developer.mozilla.org/en-US/docs/Web/API/…
– Chris G
Nov 11 '18 at 23:34




developer.mozilla.org/en-US/docs/Web/API/…
– Chris G
Nov 11 '18 at 23:34












1 Answer
1






active

oldest

votes


















-1














First, you have to create an image with the src attribute of the image you want.



<img src='sampleimage.png' id='sampleimage'>


In the javascript, add the following code:



window.onload = function() 
var sampleImage = document.getElementById('sampleimage');



In the show function:



ctx.drawImage(sampleImage, this.x, this.y, this.w, this.h);


Here is a tutorial on the ctx.drawImage function.






share|improve this answer






















  • Maybe I'm overlooking something, however, when I followed your steps I ended up with my images stationary and not reacting like my previous squares were
    – Travis Smith
    Nov 12 '18 at 0:10











  • Sorry, on the coordinates for the image, I was using this.x for the y. I've fixed the code and it should work now.
    – Liam Strilchuk
    Nov 12 '18 at 0:19










  • onload !!!!
    – Kaiido
    Nov 12 '18 at 0:24










  • THANK YOU Liam!! That was the money maker right there! Such a big help!!
    – Travis Smith
    Nov 12 '18 at 0:35










  • The one marked duplicate or referenced as the duplicate did not fix my issue, and this one did. The question will remain the same because I couldn't find any help on here...so....yeah!
    – Travis Smith
    Nov 12 '18 at 1:01

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-1














First, you have to create an image with the src attribute of the image you want.



<img src='sampleimage.png' id='sampleimage'>


In the javascript, add the following code:



window.onload = function() 
var sampleImage = document.getElementById('sampleimage');



In the show function:



ctx.drawImage(sampleImage, this.x, this.y, this.w, this.h);


Here is a tutorial on the ctx.drawImage function.






share|improve this answer






















  • Maybe I'm overlooking something, however, when I followed your steps I ended up with my images stationary and not reacting like my previous squares were
    – Travis Smith
    Nov 12 '18 at 0:10











  • Sorry, on the coordinates for the image, I was using this.x for the y. I've fixed the code and it should work now.
    – Liam Strilchuk
    Nov 12 '18 at 0:19










  • onload !!!!
    – Kaiido
    Nov 12 '18 at 0:24










  • THANK YOU Liam!! That was the money maker right there! Such a big help!!
    – Travis Smith
    Nov 12 '18 at 0:35










  • The one marked duplicate or referenced as the duplicate did not fix my issue, and this one did. The question will remain the same because I couldn't find any help on here...so....yeah!
    – Travis Smith
    Nov 12 '18 at 1:01















-1














First, you have to create an image with the src attribute of the image you want.



<img src='sampleimage.png' id='sampleimage'>


In the javascript, add the following code:



window.onload = function() 
var sampleImage = document.getElementById('sampleimage');



In the show function:



ctx.drawImage(sampleImage, this.x, this.y, this.w, this.h);


Here is a tutorial on the ctx.drawImage function.






share|improve this answer






















  • Maybe I'm overlooking something, however, when I followed your steps I ended up with my images stationary and not reacting like my previous squares were
    – Travis Smith
    Nov 12 '18 at 0:10











  • Sorry, on the coordinates for the image, I was using this.x for the y. I've fixed the code and it should work now.
    – Liam Strilchuk
    Nov 12 '18 at 0:19










  • onload !!!!
    – Kaiido
    Nov 12 '18 at 0:24










  • THANK YOU Liam!! That was the money maker right there! Such a big help!!
    – Travis Smith
    Nov 12 '18 at 0:35










  • The one marked duplicate or referenced as the duplicate did not fix my issue, and this one did. The question will remain the same because I couldn't find any help on here...so....yeah!
    – Travis Smith
    Nov 12 '18 at 1:01













-1












-1








-1






First, you have to create an image with the src attribute of the image you want.



<img src='sampleimage.png' id='sampleimage'>


In the javascript, add the following code:



window.onload = function() 
var sampleImage = document.getElementById('sampleimage');



In the show function:



ctx.drawImage(sampleImage, this.x, this.y, this.w, this.h);


Here is a tutorial on the ctx.drawImage function.






share|improve this answer














First, you have to create an image with the src attribute of the image you want.



<img src='sampleimage.png' id='sampleimage'>


In the javascript, add the following code:



window.onload = function() 
var sampleImage = document.getElementById('sampleimage');



In the show function:



ctx.drawImage(sampleImage, this.x, this.y, this.w, this.h);


Here is a tutorial on the ctx.drawImage function.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 '18 at 0:26

























answered Nov 11 '18 at 23:54









Liam Strilchuk

12915




12915











  • Maybe I'm overlooking something, however, when I followed your steps I ended up with my images stationary and not reacting like my previous squares were
    – Travis Smith
    Nov 12 '18 at 0:10











  • Sorry, on the coordinates for the image, I was using this.x for the y. I've fixed the code and it should work now.
    – Liam Strilchuk
    Nov 12 '18 at 0:19










  • onload !!!!
    – Kaiido
    Nov 12 '18 at 0:24










  • THANK YOU Liam!! That was the money maker right there! Such a big help!!
    – Travis Smith
    Nov 12 '18 at 0:35










  • The one marked duplicate or referenced as the duplicate did not fix my issue, and this one did. The question will remain the same because I couldn't find any help on here...so....yeah!
    – Travis Smith
    Nov 12 '18 at 1:01
















  • Maybe I'm overlooking something, however, when I followed your steps I ended up with my images stationary and not reacting like my previous squares were
    – Travis Smith
    Nov 12 '18 at 0:10











  • Sorry, on the coordinates for the image, I was using this.x for the y. I've fixed the code and it should work now.
    – Liam Strilchuk
    Nov 12 '18 at 0:19










  • onload !!!!
    – Kaiido
    Nov 12 '18 at 0:24










  • THANK YOU Liam!! That was the money maker right there! Such a big help!!
    – Travis Smith
    Nov 12 '18 at 0:35










  • The one marked duplicate or referenced as the duplicate did not fix my issue, and this one did. The question will remain the same because I couldn't find any help on here...so....yeah!
    – Travis Smith
    Nov 12 '18 at 1:01















Maybe I'm overlooking something, however, when I followed your steps I ended up with my images stationary and not reacting like my previous squares were
– Travis Smith
Nov 12 '18 at 0:10





Maybe I'm overlooking something, however, when I followed your steps I ended up with my images stationary and not reacting like my previous squares were
– Travis Smith
Nov 12 '18 at 0:10













Sorry, on the coordinates for the image, I was using this.x for the y. I've fixed the code and it should work now.
– Liam Strilchuk
Nov 12 '18 at 0:19




Sorry, on the coordinates for the image, I was using this.x for the y. I've fixed the code and it should work now.
– Liam Strilchuk
Nov 12 '18 at 0:19












onload !!!!
– Kaiido
Nov 12 '18 at 0:24




onload !!!!
– Kaiido
Nov 12 '18 at 0:24












THANK YOU Liam!! That was the money maker right there! Such a big help!!
– Travis Smith
Nov 12 '18 at 0:35




THANK YOU Liam!! That was the money maker right there! Such a big help!!
– Travis Smith
Nov 12 '18 at 0:35












The one marked duplicate or referenced as the duplicate did not fix my issue, and this one did. The question will remain the same because I couldn't find any help on here...so....yeah!
– Travis Smith
Nov 12 '18 at 1:01




The one marked duplicate or referenced as the duplicate did not fix my issue, and this one did. The question will remain the same because I couldn't find any help on here...so....yeah!
– Travis Smith
Nov 12 '18 at 1:01



Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20