admin管理员组

文章数量:1421432

I am trying to get a shape to follow the mouse cursor on a large canvas. This code isn't working as expected. How can I modify this to the relative canvas size if the canvas is 100% width / height of the screen?

Also, I am aware of requestanimationframe, but for the purpose of demonstrating this issue, please ignore it (I'm using it but its a plicated mess of timings that aren't related to this example).

Fiddle: /

CSS:

* { margin: 0; padding: 0;}

body, html { height:100%; }

canvas {
    image-rendering: -moz-crisp-edges;    /* Firefox */
    image-rendering: pixelated;           /* Chrome */
    position:absolute;
    width:100%;
    height:100%;
}

HTML/JS:

<!DOCTYPE html>
<html>
    <head>
        <link href="main.css" rel="stylesheet"</link>
    </head>
    <body>
        <div id="wrapper">
            <canvas id="ctx" width="256" height="144"></canvas>
        </div>
        <script>    
            var mouseX, mouseY;
            const WIDTH = 256;
            const HEIGHT = 144;
            var ctx = document.getElementById('ctx').getContext('2d');
            ctx.canvas.addEventListener('mousemove', setMousePosition, false);
            function setMousePosition(e) {
                mouseX = e.clientX;
                mouseY = e.clientY;
            }
            function drawCursor() {
                ctx.clearRect(0,0,WIDTH,HEIGHT);
                ctx.fillStyle = "#FF6A6A";
                ctx.fillRect(mouseX, mouseY, 16, 16);
            }
            setInterval(function (){
                drawCursor();
            }, 40);
        </script>
    </body>
</html>

Thank you for any and all help.

I am trying to get a shape to follow the mouse cursor on a large canvas. This code isn't working as expected. How can I modify this to the relative canvas size if the canvas is 100% width / height of the screen?

Also, I am aware of requestanimationframe, but for the purpose of demonstrating this issue, please ignore it (I'm using it but its a plicated mess of timings that aren't related to this example).

Fiddle: https://jsfiddle/gs5a4z84/

CSS:

* { margin: 0; padding: 0;}

body, html { height:100%; }

canvas {
    image-rendering: -moz-crisp-edges;    /* Firefox */
    image-rendering: pixelated;           /* Chrome */
    position:absolute;
    width:100%;
    height:100%;
}

HTML/JS:

<!DOCTYPE html>
<html>
    <head>
        <link href="main.css" rel="stylesheet"</link>
    </head>
    <body>
        <div id="wrapper">
            <canvas id="ctx" width="256" height="144"></canvas>
        </div>
        <script>    
            var mouseX, mouseY;
            const WIDTH = 256;
            const HEIGHT = 144;
            var ctx = document.getElementById('ctx').getContext('2d');
            ctx.canvas.addEventListener('mousemove', setMousePosition, false);
            function setMousePosition(e) {
                mouseX = e.clientX;
                mouseY = e.clientY;
            }
            function drawCursor() {
                ctx.clearRect(0,0,WIDTH,HEIGHT);
                ctx.fillStyle = "#FF6A6A";
                ctx.fillRect(mouseX, mouseY, 16, 16);
            }
            setInterval(function (){
                drawCursor();
            }, 40);
        </script>
    </body>
</html>

Thank you for any and all help.

Share Improve this question edited Sep 29, 2016 at 15:46 simon asked Sep 29, 2016 at 13:30 simonsimon 9642 gold badges10 silver badges24 bronze badges 2
  • Wanted to also ment, in the css if I remove width:100% and height: 100%, it will work as how I want it, but the thing is as far as rendering goes, I need a canvas that will be stretched, is there a way around this? – simon Commented Sep 29, 2016 at 14:54
  • BTW, the link element can't have a closing element, instead of <link stuff></link> use <link stuff> or <link stuff/> – Bálint Commented Sep 29, 2016 at 15:51
Add a ment  | 

1 Answer 1

Reset to default 3

If you use CSS width and height property for the canvas object, then you won't actually change how many pixels there are on the canvas, but how big it is. To fix your problem, set the canvas' width and height property using JavaScript:

JavaScript:

ctx.canvas.width = innerWidth;
ctx.canvas.height = innerHeight;

CSS:

body, canvas {
    margin: 0;
    padding: 0;
}

本文标签: javascriptdrawing to responsive canvas that is 100 width and heightStack Overflow