Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
45
rated 0 times [  47] [ 2]  / answers: 1 / hits: 32436  / 10 Years ago, fri, march 7, 2014, 12:00:00

I work at a web application for painting images. I use a CANVAS element and JavaScript to draw on it, but I have a problem: how can I load an image from the PC of the user and draw it on the canvas? I don't want to save it on the server, only on the webpage.


Here is a shortened version of the code:


HTML:


Open file: <input type="file" id="fileUpload" accept="image/*" /><br />
<canvas id="canvas" onmousemove="keepLine()" onmouseup="drawLine()" onmousedown="startLine()" width="900" height="600" style="background-color:#ffffff;cursor:default;">
Please open this website on a browser with javascript and html5 support.
</canvas>

JavaScript:


var x = 0;
var y = 0;
var clicked = false;

var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");

context.strokeStyle = "black";
context.lineCap = "round";

canvas.addEventListener('mousemove', function(e) { getMousePos(canvas, e); }, false);

takePicture.onchange = function (event) {
var files = event.target.files,
file;
if (files && files.length > 0) {
file = files[0];
processImage(file);
}
};

function processImage(file) {
var reader = new FileReader();
reader.readAsDataURL(file)
reader.onload = function(e) {
var img = new Image();
img.onload = function() {
context.drawImage(img, 100,100)
}
img.src = e.target.result;
}
}

// functions for drawing (works perfectly well)
function getMousePos(canvas, e) {
var rect = canvas.getBoundingClientRect();
x = evt.clientX - rect.left;
y = evt.clientY - rect.top;
}

function startLine() {
context.moveTo(x,y);
context.beginPath();
clicked = true;
}

function keepLine() {
if(clicked) {
context.lineTo(x,y);
context.stroke();
context.moveTo(x,y);
}
}

function drawLine() {
context.lineTo(x,y);
context.stroke();
clicked = false;
}

There's no copyright


More From » html

 Answers
15



const EL = (sel) => document.querySelector(sel);
const ctx = EL(#canvas).getContext(2d);

function readImage() {
if (!this.files || !this.files[0]) return;

const FR = new FileReader();
FR.addEventListener(load, (evt) => {
const img = new Image();
img.addEventListener(load, () => {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
ctx.drawImage(img, 0, 0);
});
img.src = evt.target.result;
});
FR.readAsDataURL(this.files[0]);
}

EL(#fileUpload).addEventListener(change, readImage);

canvas {display: block;}

<input type='file' id=fileUpload />
<canvas id=canvas width=300 height=200></canvas>




[#72102] Wednesday, March 5, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sidneyh

Total Points: 118
Total Questions: 108
Total Answers: 105

Location: Mali
Member since Fri, Jun 18, 2021
3 Years ago
sidneyh questions
Tue, Jun 7, 22, 00:00, 2 Years ago
Wed, Apr 13, 22, 00:00, 2 Years ago
Wed, Aug 12, 20, 00:00, 4 Years ago
Wed, Jun 3, 20, 00:00, 4 Years ago
Fri, Apr 24, 20, 00:00, 4 Years ago
;