首页 > 其他 > 详细

Canvas缩放图像

时间:2016-03-07 22:41:33      阅读:258      评论:0      收藏:0      [点我收藏+]
<body>
<canvas id="canvas"></canvas>
<input type="range" id="scale_range" min="0.5" max="3" step="0.01" value="1.0">
<script>
    slider = document.getElementById("scale_range");
    canvas = document.getElementById("canvas");
    context = canvas.getContext("2d");
    var image = new Image();
    window.onload = function(){
        canvas.width = 500;
        canvas.height = 500;
        image.src = "/a.png";
        var scale = slider.value;
        image.onload = function(){
            drawByScale(scale);
            slider.onmousemove = function(){
                scale = slider.value;
                drawByScale(scale);
            }

        }
    }
    function drawByScale(scale){
        var imageWidth = canvas.width*scale;
        var imageHeight = canvas.height*scale;
        //var sx = imageWidth/2-canvas.width/2;
        //var sy = imageHeight/2-canvas.height/2;
        var dx = canvas.width/2-imageWidth/2;
        var dy = canvas.height/2-imageHeight/2;
        context.clearRect(0,0,canvas.width,canvas.height);
        context.drawImage(image,dx,dy,imageWidth,imageHeight);
    }
</script>
</body>

Canvas缩放图像

原文:http://blog.csdn.net/silencemylove/article/details/50821485

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!