【发布时间】:2016-11-08 15:48:55
【问题描述】:
我正在尝试为课堂做作业,基本上它必须是“互动的”。我想使用 mousemove 语法来更改我制作的线条的颜色和大小。我在 html 文件的 div 中创建了行,并使用 getElementById 语法来创建行。
我有线条...我不知道如何让它们移动和改变颜色。我的教授给我发了代码,当鼠标移到线条上时,他让线条改变颜色。我理解代码,但我不知道当鼠标移到线条上时如何让线条随机移动和改变大小。
我是否需要为每一行创建单独的 div 以使它们移动、随机更改颜色和随机更改大小,或者我可以只做我所做的,用多行创建一个 div 并让它们做什么我想要,彼此独立?
下面是我的代码的链接!
谢谢!!!!!!
[1]: http://codepen.io/niymil/pen/pNoOqz
var w = 100;
var h = 500;
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
function adjustLineStyle(y, lineY) {
var distance = Math.abs(lineY - y);
var lightness = 100 - distance;
// hsl makes a color HUE, SATURATION, LIGHTNESS.
// lightness will be how far Y is from the Y of line.
ctx.strokeStyle = "hsl(80, 70%," + lightness + "%)";
ctx.lineWidth = 2;
};
function clear() {
ctx.fillStyle = 'hsla(0,0%,0%,0.1)';
ctx.fillRect(0,0,500,500);
}
var startX = 0 ;
var endX = 500 ;
function drawlines(mouseEvent) {
var mouseY = mouseEvent.offsetY;
startX = startX + (Math.random() -0.5) * 30;
endX = endX + (Math.random() - 0.5) * 30;
ctx.strokeStyle = 'white'
ctx.beginPath();
ctx.moveTo(startX, mouseY);
ctx.lineTo(endX, mouseY);
ctx.stroke();
}
setInterval(clear,50);
document.addEventListener('mousemove', drawlines);
//draw lines as as the mouse is hovered over the lines
//the lines are supposed to change size as the mouse is hovered over the canvas
//as lines reappear, they should change color randomly
【问题讨论】:
-
您能否将您的代码复制/粘贴到实际问题中?当您点击问题底部的
edit时,您可以点击小< >图标来完成此操作 -
我没有看到任何线条...我在这里遗漏了什么吗?
-
干得好.. 你快到了.. 只是错过了一件小事.. 尝试在某处添加
$(document).ready(function(){ animateDiv(); });就可以了
标签: javascript jquery html canvas onmousemove