首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调整宽高比图像大小时出现excanvas错误

调整宽高比图像大小时出现excanvas错误
EN

Stack Overflow用户
提问于 2012-03-03 06:16:50
回答 1查看 560关注 0票数 0

下面的代码创建了全屏图像,并具有合适的比例。但这不适用于ie和excanvas。我不能解决这个问题。有什么帮助吗?

这是jsfiddle链接:http://jsfiddle.net/salt/Zs6uV/

解决方案:http://jsfiddle.net/salt/xpwZh/

代码语言:javascript
复制
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <title>aspectratio test</title>
<script type="text/javascript">
        function getWindowSize(typ) {
        var myWidth = 0, myHeight = 0;
        if( typeof( window.innerWidth ) == 'number' ) {
          //Non-IE
          myWidth = window.innerWidth;
          myHeight = window.innerHeight;
        } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
          //IE 6+ in 'standards compliant mode'
          myWidth = document.documentElement.clientWidth;
          myHeight = document.documentElement.clientHeight;
        } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
          //IE 4 compatible
          myWidth = document.body.clientWidth;
          myHeight = document.body.clientHeight;
        }
        if(typ=="width"){
            return myWidth;
        }else{
            return myHeight;
        };
    };
</script>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
        <!--[if IE]><script type="text/javascript" src="http://explorercanvas.googlecode.com/svn/trunk/excanvas.js"></script><![endif]-->
    <!--[if lt IE 9]><script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
      </head>
      <body style="margin:0px;"  onLoad="setupBackground();">
         <canvas id="myCanvas" style="position:absolute;left: 0px; top: 0px; z-index: 1;"></canvas>
    <script type="text/javascript">
    function setupBackground() {
         canvas = document.getElementById('myCanvas');
        if (typeof window.G_vmlCanvasManager!="undefined") { 
            canvas=window.G_vmlCanvasManager.initElement(canvas);
            var ctx = canvas.getContext('2d');
        }else{
            var ctx = canvas.getContext('2d');
        };
        function draw() {
            canvas.width = 0;
            canvas.height = 0;

            var divWidth = getWindowSize("width"); 
            var divHeight = getWindowSize("height"); 

            var yScale = divHeight / img.height;
            var xScale = divWidth / img.width;

            var newImgHeight = img.height * xScale;
            var newImgWidth = divWidth;

            if (divHeight >= newImgHeight) {
                newImgHeight = divHeight;
                newImgWidth = img.width * yScale;
            };

            canvas.width = divWidth;
            canvas.height = divHeight;

        var diffX =(Math.max(newImgWidth,divWidth)-Math.min(newImgWidth,divWidth))/2;
        var diffY =(Math.max(newImgHeight,divHeight)-Math.min(newImgHeight,divHeight))/2;
        var imgX=0-diffX;
        var imgY=0-diffY;
            ctx.drawImage(img,imgX,imgY,newImgWidth,newImgHeight);
        };

        var img = new Image();
        img.onload = function() {
            $(window).bind('resize', function() {
            draw();
        });
            draw();
        };
        img.src ='http://userserve-ak.last.fm/serve/_/460496/Popperklopper.jpg';
    };
    </script>
    </body>
    </html>
EN

回答 1

Stack Overflow用户

发布于 2012-05-20 21:51:14

不确定这是不是你的问题,但是当你这样做的时候:

代码语言:javascript
复制
<!--[if IE]><script type="text/javascript" src="http://explorercanvas.googlecode.com/svn/trunk/excanvas.js"></script><![endif]-->
    <!--[if lt IE 9]><script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->

应该是:

代码语言:javascript
复制
<!--[if IE]--><script type="text/javascript" src="http://explorercanvas.googlecode.com/svn/trunk/excanvas.js"></script><!--[endif]-->
        <!--[if lt IE 9]--><script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script><!--[endif]-->

你保留了你的脚本文档,所以它不能在IE版本上工作,希望它能解决你的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9541137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档