Basically the title is my question. I have no code to show (I wouldn't know where to start) and all of this needs to be in pure javascript.
Asked
Active
Viewed 346 times
-1
-
1You might want to start with Google to find this: https://stackoverflow.com/questions/22255580/javascript-upload-image-file-and-draw-it-into-a-canvas. – Aron Aug 10 '17 at 20:45
-
Welcome to Stack Overflow! Questions that ask "where do I start" are typically too broad and are not a good fit for this site. People have their own method for approaching the problem and because of this there cannot be a _correct_ answer. Give a good read over [**Where to Start**](https://softwareengineering.meta.stackexchange.com/questions/6366/where-to-start/6367#6367), then address your post. – 4castle Aug 10 '17 at 20:45
-
@Aron Thanks for linking, next time you can flag this question as duplicate too – Alon Eitan Aug 10 '17 at 20:46
1 Answers
0
Look up the drawImage function (i recommend w3schools).
In your javascript you first need to get the canvas
const canvas = document.getElementById("canvasID");
And theb the context of the canvas
const context = canvas.getContext("2d");
The context is what you want to call the drawImage function on.
context.drawImage(image, xPos, Ypos);
The image that you pass in the drawImage should be loaded this way:
const image = new Image();
image.src = "path/image.jpg";
image.addEventListener('load', () =>
{
context.drawImage(image, xPos, yPos);
});
The way I would do it is as following:
window.addEventListener('load', () =>
{
const canvas = document.getElementById("canvasID");
const ctx = canvas.getContext("2d");
const image = new Image();
image.addEventListener('load', () =>
{
ctx.drawImage(image, xPos, yPos);
});
});

danivdwerf
- 234
- 2
- 16
-
That doesn't work. Here's my code: window.onload = function() { var canvas = document.createElement("canvas"); var c = canvas.getContext("2d"); canvas.width = 400; canvas.height = 400; document.body.appendChild(canvas); c.fillStyle = "white"; c.fillRect(0,0,canvas.width,canvas.height); var image = new Image(); image.src = "C:\Users\Kids\Desktop\Game Maker Stuff\Resources\Modified Sprite Resources\Gaster Boss Fight\gaster\gaster_full"; image.addEventListener('load', () => { context.drawImage(image, xPos, yPos); }); c.drawImage(image,0,0); } – Canvas16 Aug 10 '17 at 20:53
-
The xPos and yPos arent values, you need to replace them with actual numbers. Also try to use paths relative to.the html file. So if you have your html file in a folder, and the image in a folder the path would be: ../images/image.png. remember that the path will be from the html file, even though if your html is in a different file than the html – danivdwerf Aug 10 '17 at 20:57
-
I don't actually have an html file. This is my first venture into multi-file programs. @Aron That doesn't work for me because that program requires manual upload every time it is run. I want a program that always uploads the same image automatically. – Canvas16 Aug 10 '17 at 21:03
-
-
-