jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – HTML5 Canvas调整图像点击放大大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在画布中有一张地图的图像,我希望使用 HTML5和jQuery作为谷歌地图界面.我想执行以下操作:

• Double click on pan in without changing the size of the containing div

• Click on a building to bring up a pop up of information on that building

• Click and drag to pan

我现在的代码是画布中的图像.我发现几个代码显示“缩放”图像,但要么调整容器div的大小,要么不是一个平滑和响应的方式,它只是跳到更大的尺寸,我希望缩放动画.

地图上会有几个“固定”的建筑物,用户可以点击这些建筑物来显示有关该建筑物的信息.

代码如下:

JS:

$( document ).ready( function() {
var canvas = document.getElementById( 'canvas' );
var context = null;
var map = null;

if( canvas.getContext )
{
    context = canvas.getContext( '2d' );

    $( '#map' ).load( function( evt ) {
        context.drawImage( evt.currentTarget,10,10 );
    } );

    map = new Image();
    map.onload = function() {
        context.drawImage( this,20,20 );
    };
    map.src = 'images/asu_poly_map.png';
} else {
    alert( 'Your browser does not support canvas.' );
}
} );

HTML:

<div id="map">

<canvas id="canvas" width="1055" height="600"></canvas>

</div>

CSS:

#map {
margin:0 auto;
margin-top : 180px;
width : 1200px;
}

编辑添加
我假设它会与此类似,但无法弄清楚如何将它应用于从绘制到画布上的服务器的图像.

Zoom in on a point (using scale and translate)

解决方法

繁荣!我将一堆StackOverflow引用组合成适用于你的东西: http://jsfiddle.net/CMse5/1/

这是代码

HTML:

<div id="map">
    <canvas id="canvas" width="300" height="300"></canvas>
</div>

CSS:

canvas {
    border: 1px solid black;
}

JS:

$( document ).ready( function() {
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
var scale = 1.5;
var originx = 0;
var originy = 0;
var imageObj = new Image(); 
    imageObj.src = 'http://placehold.it/300x300';

function draw(){
    // From: http://goo.gl/jypct
    // Store the current transformation matrix
    context.save();

    // Use the identity matrix while clearing the canvas
    context.setTransform(1,1,0);
    context.clearRect(0,canvas.width,canvas.height);

    // Restore the transform
    context.restore();

    // Draw on transformed context    
    context.drawImage(imageObj,300,300);

}
seTinterval(draw,100);

canvas.onmousewheel = function (event){
    var mousex = event.clientX - canvas.offsetLeft;
    var mousey = event.clientY - canvas.offsetTop;
    var wheel = event.wheelDelta/120;//n or -n


    //according to Chris comment
    var zoom = Math.pow(1 + Math.abs(wheel)/2,wheel > 0 ? 1 : -1);

    context.translate(
        originx,originy
    );
    context.scale(zoom,zoom);
    context.translate(
        -( mousex / scale + originx - mousex / ( scale * zoom ) ),-( mousey / scale + originy - mousey / ( scale * zoom ) )
    );

    originx = ( mousex / scale + originx - mousex / ( scale * zoom ) );
    originy = ( mousey / scale + originy - mousey / ( scale * zoom ) );
    scale *= zoom;
}

} );

此外,您还提到了以平滑方式动画缩放的动画.为此,您需要简化每帧的缩放.因此,在您的绘图功能中,您希望在原始比例与新比例之间放松每个帧.您肯定希望更改为requestAnimationFrame,以便获得更好的帧速率.

使用canvas / DOM混合可能更简单,并在图像上使用CSS3转换和动画,它可以为您完成所有缓动.如果需要,您可以覆盖任何必要的画布.

大佬总结

以上是大佬教程为你收集整理的jquery – HTML5 Canvas调整图像点击放大全部内容,希望文章能够帮你解决jquery – HTML5 Canvas调整图像点击放大所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。