2015-09-26 45 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); 
    }