简体   繁体   English

如何使用键盘旋转画布框?

[英]How to rotate a canvas box using keyboard?

I've been wondering if there is a way to turn a canvas box using the keyboard without using animations. 我一直想知道是否有一种方法可以使用键盘而不使用动画来打开画布框。

I would like to press the key E to turn my box clockwise, and key Q should turn it counter-clockwise. 我想按E键以顺时针旋转我的盒子,而Q键则应逆时针旋转它。 As I said before, using no animations . 如我之前所说,不使用任何动画

If you would like to see exactly what I'm working on, and the context where it should be done, here is a link http://cssdeck.com/labs/collab/stexplorer 如果您想确切了解我在做什么,应该在哪里进行操作,请访问以下链接http://cssdeck.com/labs/collab/stexplorer

and just in case, I'll post my code here too! 以防万一,我也将代码发布在这里!

If you use this code, notice that I want it to be able to turn and move forward at the same time . 如果您使用此代码,请注意,我希望它能够打开,并同一时间向前推进。

 $(function() { var n = 3; var xD = 0; var yD = 0; var btn = undefined; var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); window.addEventListener('resize', resizeCanvas, false); function resizeCanvas() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; render(); } var ss = { "x": 0, "y": 0, "width": 100, "height": 75 }; function render() { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.beginPath(); ctx.rect(ss.x, ss.y, ss.width, ss.height); ctx.lineWidth = 1; ctx.strokeStyle = "white"; ctx.stroke(); } function move() { x = ss.x + (xD * n); y = ss.y + (yD * n); ss.x = x; ss.y = y; render(); } $(document).keydown(function(e) { if(btn !== undefined){ return; } // shoot (space):32 // left xD = e.which == 37 ? -1 : xD; xD = e.which == 65 ? -1 : xD; // up yD = e.which == 38 ? -1 : yD; yD = e.which == 87 ? -1 : yD; // right xD = e.which == 39 ? 1 : xD; xD = e.which == 68 ? 1 : xD; // down yD = e.which == 40 ? 1 : yD; yD = e.which == 83 ? 1 : yD; // clockwise e:69 // counter-clockwise q: 81 // zoom-out f:70 // zoom-in r:82 btn = e.which; e.preventDefault(); }); $(document).keyup(function(e) { if(e.which === btn){ btn = undefined; } // shoot (space):32 // left xD = e.which == 37 ? 0 : xD; xD = e.which == 65 ? 0 : xD; // up yD = e.which == 38 ? 0 : yD; yD = e.which == 87 ? 0 : yD; // right xD = e.which == 39 ? 0 : xD; xD = e.which == 68 ? 0 : xD; // down yD = e.which == 40 ? 0 : yD; yD = e.which == 83 ? 0 : yD; // clockwise e:69 // counter-clockwise q: 81 // zoom-out f:70 // zoom-in r:82 e.preventDefault(); }); resizeCanvas(); render(); setInterval(move, .01); }); 
 body { margin: 0; overflow: hidden; } #canvas { border: 1px solid #000000; background-color: black; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <canvas id="canvas" width="300" height="200"></canvas> 

If you want the rect to rotate around it's centerpoint, you must: 如果要使rect围绕其中心点旋转,则必须:

  • Set the rotation point to the center of the rect using context.translate 使用context.translate将旋转点设置为矩形的中心
  • Rotate the canvas using context.rotate 使用context.rotate旋转画布
  • Draw the rect 画矩形

Here's (untested) example code: 这是(未经测试的)示例代码:

var accumRotation=0;

// now change the accumRotation using the E & Q keys
// on key-E do rotate(Math.PI/2); and render();
// on key-Q do rotate(-Math.PI/2); and render();

function rotate(additionalRotation){
    accumRotation+=additionalRotation;
}

function render() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    // translate to the centerpoint of the rect
    // This sets the [0,0] origin of the canvas to the rect centerpoint
    // This will set the rotation point at the center of the rect
    var cx=ss.x+ss.width/2;
    var cy=ss.y+ss.height/2;
    ctx.translate(cx,cy);
    // rotate
    ctx.rotate(accumRotation);
    // draw the rect
    ctx.beginPath();
    // since [0,0] is at center rect, you must pull the rect drawing
    // leftward & upward by half the rect width & height
    ctx.rect(-ss.width/2, -ss.height/2, ss.width, ss.height);
    ctx.lineWidth = 1;
    ctx.strokeStyle = "white";
    ctx.stroke();
    // always clean up by unrotating & untranslating
    ctx.rotate(-accumRotation);
    ctx.translate(-cx,-cy);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM