【问题标题】:rotate rectangle with mouse scroll kineticjs用鼠标滚动 kineticjs 旋转矩形
【发布时间】:2013-10-16 04:26:36
【问题描述】:

如何使用 kineticjs 用鼠标滚动来旋转矩形? 下面的链接显示了在鼠标按下时使用 jquery 旋转形状。但我想使用鼠标滚轮事件而不是 mousePos,因为我的矩形是可拖动的。 http://www.lonhosford.com/content/html5/canvas/rotate_to_mouse.html

【问题讨论】:

  • 我为任何感兴趣的人设置了一个 jsFiddle:jsfiddle.net/MrPolywhirl/FXb6P
  • @Mr.Polywhirl:+1 以获得形状自动旋转到鼠标位置的精彩演示。
  • 感谢您的回复,但我想使用 KineticJS 旋转矩形

标签: javascript html html5-canvas kineticjs


【解决方案1】:

KineticJS 本身并不跟踪鼠标滚轮事件。

可以使用jQuery+鼠标滚轮插件来接收鼠标滚轮事件。

<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-mousewheel/3.1.3/jquery.mousewheel.min.js"></script>

为什么选择 jQuery?

您可以在没有 jQuery+插件的情况下跟踪鼠标滚轮事件,但鼠标滚轮事件尚未跨浏览器标准化,因此跨浏览器的非 jQuery 解决方案会很复杂。

以下是在舞台容器上监听鼠标滚轮事件并旋转 Rect 的方法:

// set this variable to refer to the KineticJS rect you want to rotate

var theRectToRotate    

// set this variable to the number of degrees to rotate when the user mousewheels

var degreeRotationPerMousewheelDelta=5;

// Use jQuery to listen for mousewheel events
// Then rotate the specified rect by the specified degree

$("#container").mousewheel(function(event, delta, deltaX, deltaY){
   theRectToRotate.rotateDeg(delta* degreeRotationPerMousewheelDelta );
   layer.draw();
   event.preventDefault();
});

这是代码和小提琴:http://jsfiddle.net/m1erickson/kXJ5q/

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Prototype</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
    <script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.7.0.min.js"></script>
    <script src="="//cdnjs.cloudflare.com/ajax/libs/jquery-mousewheel/3.1.3/jquery.mousewheel.min.js"></script>

<style>
#container{
  border:solid 1px #ccc;
  margin-top: 10px;
  width:300px;
  height:300px;
}
</style>        
<script>
$(function(){

    var stage = new Kinetic.Stage({
        container: 'container',
        width: 300,
        height: 300
    });
    var layer = new Kinetic.Layer();
    stage.add(layer);


    $("#container").mousewheel(function(event, delta, deltaX, deltaY){
       console.log(delta+": "+deltaX+"/"+deltaY); 
       rect.rotateDeg(delta*5);
       layer.draw();
       event.preventDefault();
    });

    var rect = new Kinetic.Rect({
        x: 100,
        y: 100,
        width: 100,
        height: 70,
        offset:[50,35],
        fill: 'skyblue',
        stroke: 'lightgray',
        strokeWidth: 3
    });
    layer.add(rect);


    layer.draw();


}); // end $(function(){});

</script>       
</head>

<body>
    <h3>Use mousewheel to rotate the rect</h3>
    <h5>This requires a plugin:</h5> 
    <h5>https://github.com/brandonaaron/jquery-mousewheel </h5>
    <div id="container"></div>
</body>
</html>

【讨论】:

  • markE:感谢以上回答。您的代码正在运行,但您能否为我提供一个使用 KineticJS 用于 IE9 和 IE10 的解决方案?
  • 是的,但我更喜欢使用 KinetcJS
  • 我只关心 IE。 Chrome 支持将是一个奖励,但对其他浏览器的支持并不重要。
  • 我的示例小提琴在 IE9+ 中正常工作,它使用 KineticJS 库在 html 画布中绘制形状。
  • 是的,但是可以使用纯 Kineticjs 即鼠标滚动来旋转。没有 jquery 或任何其他 javascript 库?
【解决方案2】:

假设您有办法知道当前聚焦/选择了哪个矩形(或者您希望它们同时旋转)。最简单的方法是让它不连续,即为每个鼠标滚轮事件旋转一定的量。

为此,您需要获取事件的 wheelDelta,检查它是正还是负,然后像 rect.setRotation(mySetAngleInRadians * isNegativeAngle) 那样将矩形旋转一个设定的角度,然后重新绘制图层。

如果您想要连续滚动,那么您需要handle the cases where it isn't supported 并确定对应于 360 度(整圈)的滚动距离值,然后执行以下操作:

rect.setRotation(ev.wheelDelta/fullScrollDistance * 2 * Math.PI)

【讨论】:

  • 嘿,qterton,你能解释一下这些条款吗?特别是 fullScrollDistance。它应该是高还是低,我想围绕它们的中心旋转矩形。是的,我知道如何选择一个矩形。
猜你喜欢
  • 1970-01-01
  • 2012-12-29
  • 1970-01-01
  • 2013-08-11
  • 2012-03-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-03
相关资源
最近更新 更多