---
# System prepended metadata

title: canvas

---

# canvas

```
var zipBaseImg = function(i, w, h, callback) {
        w = w || 400;
        h = h || 300;

        var img = document.createElement("img"); //创建临时图片
        img.src = i;

        var canvas = document.createElement("canvas"); //创建临时画布
        canvas.width = w;
        canvas.height = h;

        var ctx = canvas.getContext("2d");

        img.onload = function() {
          //圖片加載結束後才執行這個函式！
          ctx.drawImage(img, 0, 0, w, h); //建構臨時圖片尺寸從0,0到w,h px
          if (callback && typeof callback == "function")
            return callback(canvas.toDataURL("image/jpeg", 0.8)); //第二个參數壓縮質量
        };
      };
      
      
      
zipBaseImg(e.target.result, 400, 300, function(dataurl) {  });
```