2015-09-26 98 views

回答

0

獲取您在圖像中顯示的內容。

// ctx is the canvas 2d context 
// img is the image 
function showImageScaled(img,scale,x,y){ 
    // scale is how big you want it. 
    // x and y are where in the image you want the top left to be 
    ctx.setTransform(scale,0,0,scale,0,0); 
    ctx.drawImage(img,-x,-y); // move the image 
} 

所以查看右上角放大2倍

showImageScaled(img,2,0,0); 

要顯示在左上角的中心和圖像縮放4倍

showImageScaled(img,4,img.width/2,img.height/2); 

希望這有助於。

+0

這確實有訣竅:) thx爲您提供幫助 – user3410823

0

Blindman67

給出你可以使用這個精確數字變焦,它可以擴展您在自定義縮放中央點圖像(相同的方式,LCD顯示器如何數碼相機變焦),並繪製它的解決方案的替代方案。

function precisionScale(img,scale,x,y){ 
     /** Function precisionScale 
     * The function scales the image with a custom central scaling point 
     * as opposed to 0,0 (top,left) default central scaling point 
     */ 
     ctx.translate(x,y); 
     ctx.scale(scale,scale); 
     ctx.drawImage(img,-x,-y); 
     ctx.scale(-scale,-scale); 
     ctx.translate(-x,-y); 
    }