Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

html - javascript: upload image file and draw it into a canvas

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 (the full code would be too long):

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...