javascript:上传图片文件并将其绘制到画布中

时间:2014-03-07 16:33:46

标签: javascript html5 canvas image-uploading

我在网络应用程序中工作以绘制图像。我使用CANVAS元素和javascript来绘制它,但我有一个问题:如何从用户的PC加载图像并在画布上绘制?我不想将其保存在服务器上,只在网页上保存!

以下是代码的缩写版本(完整代码太长):

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;
}

没有版权

2 个答案:

答案 0 :(得分:24)

function el(id){return document.getElementById(id);} // Get elem by ID

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

function readImage() {
    if ( this.files && this.files[0] ) {
        var FR= new FileReader();
        FR.onload = function(e) {
           var img = new Image();
           img.addEventListener("load", function() {
             context.drawImage(img, 0, 0);
           });
           img.src = e.target.result;
        };       
        FR.readAsDataURL( this.files[0] );
    }
}

el("fileUpload").addEventListener("change", readImage, false);
<input type='file' id="fileUpload" />
<canvas id="canvas" width="900" height="600"></canvas>  

答案 1 :(得分:3)

<html>
<head>
<script type="text/javascript" src="http://fiddle.jshell.net/js/lib/mootools-core-1.4.5-nocompat.js"></script>
<script type="text/javascript">
//<![CDATA[ 
window.addEvent('load', function() {
var imageLoader = document.getElementById('imageLoader');
    imageLoader.addEventListener('change', handleImage, false);
var c = document.getElementById('myCanvas');
var ctx = c.getContext('2d');


function handleImage(e){
    var reader = new FileReader();
    reader.onload = function(event){
        var img = new Image();
        img.onload = function(){
            c.width = img.width;
            c.height = img.height;
            ctx.drawImage(img,0,0);
        }
        img.src = event.target.result;
    }
    reader.readAsDataURL(e.target.files[0]);     
}

});//]]>  


</script>
</head>

<body>
<div style="background:#990; width:100%; padding:20px; ">
<label>Image File:</label><br/>
<input type="file" id="imageLoader" name="imageLoader"/>
</div>

<canvas id="myCanvas" ></canvas>
</body>
</html>