帆布用鼠标移动球

时间:2018-05-01 20:25:21

标签: javascript html5 animation canvas

我有一个鼠标图像和一个中间有线的奶酪图像。工作良好。除了看起来不错外什么也没做。

我添加了一个跟随用户鼠标的球,它清除了我的所有图像。我试图添加图层(我是一个新手)。

我希望鼠标用户能够将鼠标图像上方的球引导至奶酪。在我开始工作之后,我将把奶酪变成一个单独的图像。

鼠标用户可以将球带到任何地方。它不必只是在两个图像之间的线上。

<!DOCTYPE html>

<title>Mouse Event</title>

<style>

    canvas {
        border: #333 10px solid;
    }

</style>

<div style = "position: relative;">
    <canvas id = "layer1" width="600px" height="600px"
     style="position: absolute; left: 0; top: 0; z-index: 0;></canvas>
    <canvas id = "layer2" width="600px" height="600px"
     style="position: absolute; left: 0; top: 0; z-index: 1;></canvas>


<script>

    var canvas1 = document.querySelector("#layer1");
    var context = canvas1.getContext("2d");

    //Get the mouse position
    //First, listen for the mouse event & call setMousePosition
    //This function assigns the current horizontal and vertical mouse
    //position to the mouseX,Y properties, it relies on the clientX
    //and Y properties that the MouseEvent-based event arument object provides
    var canvasPos = getPosition(canvas);

    var mouseX = 500;
    var mouseY = 500;

    canvas1.addEventListener("mousemove", setMousePosition, false);

    function setMousePosition(e) {
      mouseX = e.clientX - canvasPos.x;//now stores the position returned by the getPosition function
      mouseY = e.clientY - canvasPos.y;
    }       

    function update() {
      context.clearRect(0, 0, canvas.width, canvas.height);//clears earlier positions
      context.beginPath();
      context.arc(mouseX, mouseY, 50, 0, 2 * Math.PI, true);
      context.fillStyle = "LightSeaGreen";
        context.lineWidth = 5;
        context.strokeStyle = "yellow";
        context.fill();
        context.stroke();

      requestAnimationFrame(update);
    }
    //Get the Exact Mouse Position
    function getPosition(el) {
      var xPosition = 0;
      var yPosition = 0;

      while (el) {
        xPosition += (el.offsetLeft - el.scrollLeft + el.clientLeft);
        yPosition += (el.offsetTop - el.scrollTop + el.clientTop);
        el = el.offsetParent;
      }
      return {
        x: xPosition,
        y: yPosition
      };
    }       

    update();

    var canvas2 = document.querySelector("#layer2");
    var context = canvas2.getContext("2d");

    //draw connecting line
    context.moveTo(30,30);
    context.bezierCurveTo(-50,600, 500, 0, 300,500);
    context.lineWidth = 15;
    context.strokeStyle = "teal";
    context.stroke();

    context.fillStyle = "#ff6600";
    context.font = "bold 35px 'Book Antiqua'";
    context.fillText("Help Me", 300,100);
    context.fillText("find the cheese,", 300, 135);
    context.fillText("Please!", 300, 170);


    //load cheeseImage
    var cheeseImage = new Image();
    cheeseImage.src = "images/transparentCheese.png";
    cheeseImage.addEventListener("load", loadImage, false);

    //load mouse image
    var mouseImage = new Image();
    mouseImage.src = "images/mouse.png";
    mouseImage.addEventListener("load", loadImage, false);


    function loadImage(e) {
        context.drawImage(cheeseImage,10,10);
        context.drawImage(mouseImage,210,400);

    }


</script>

</div>

[鼠标][1] cheese

1 个答案:

答案 0 :(得分:0)

图像消失了,因为更新功能会清除画布的每一帧(context.clearRect),但是你想要画布清除,所以不要删除它。

您需要将所有代码放在更新函数内的画布上绘制您想要的内容,或者运行更新代码调用。

最好在第一次调用update()函数之前等到两个图像都已加载。

我修改了你的代码并添加了一些评论,希望它有意义......

function update() {
  context.clearRect(0, 0, canvas.width, canvas.height);//clears earlier positions
  context.beginPath();
  context.arc(mouseX, mouseY, 50, 0, 2 * Math.PI, true);
  context.fillStyle = "LightSeaGreen";
    context.lineWidth = 5;
    context.strokeStyle = "yellow";
    context.fill();
    context.stroke();

    // Call functions to draw text and images on the canvas.
    drawText();
    drawCheese();
    drawMouse();

  requestAnimationFrame(update);
}
//Get the Exact Mouse Position
function getPosition(el) {
  var xPosition = 0;
  var yPosition = 0;

  while (el) {
    xPosition += (el.offsetLeft - el.scrollLeft + el.clientLeft);
    yPosition += (el.offsetTop - el.scrollTop + el.clientTop);
    el = el.offsetParent;
  }
  return {
    x: xPosition,
    y: yPosition
  };
}       

var canvas2 = document.querySelector("#layer2");
var context = canvas2.getContext("2d");


function drawText() {
    //draw connecting line
  context.moveTo(30,30);
  context.bezierCurveTo(-50,600, 500, 0, 300,500);
  context.lineWidth = 15;
  context.strokeStyle = "teal";
  context.stroke();

  context.fillStyle = "#ff6600";
  context.font = "bold 35px 'Book Antiqua'";
  context.fillText("Help Me", 300,100);
  context.fillText("find the cheese,", 300, 135);
  context.fillText("Please!", 300, 170);
}

// Draw cheese image on canvas
function drawCheese() {
    context.drawImage(cheeseImage,10,10);
}

// Draw mouse image on canvas.
function drawMouse() {
    context.drawImage(mouseImage,210,400);
}


//load cheeseImage
var cheeseImage = new Image();
cheeseImage.src = "images/transparentCheese.png";
cheeseImage.addEventListener("load", loadImage, false);

//load mouse image
var mouseImage = new Image();
mouseImage.src = "images/mouse.png";
mouseImage.addEventListener("load", loadImage, false);


var imagesLoaded = 0;

// Called when image is loaded.
function loadImage(e) {
        // Increment the number of images loaded
        imagesLoaded += 1;

        // If both images have loaded call the update function for the first time.
        if (imagesLoaded == 2) {
            update();
        }
}