gpt4 book ai didi

javascript - 改变鼠标移动的方差

转载 作者:数据小太阳 更新时间:2023-10-29 03:54:57 26 4
gpt4 key购买 nike

所以我在摆弄 trianglify.js。

我的目标是当鼠标移动时,我的三 Angular 形 Canvas 的方差会发生变化。

理论上很好,但实际上并没有真正采用我已经尝试了几种不同的代码格式,但实际上似乎无法进行三 Angular 化以尊重新值。

希望有人能指出我做错了什么。

//Do Traingle Canvas
window.onload = function() {
var pattern = Trianglify({
width: window.innerWidth,
height: window.innerHeight,
cell_size: 120,
stroke_width: 1.3,
variance: 0.75,
seed: '9rqsn',
x_colors: 'Blues'
});
var homecan = document.getElementById('home');
homecan.appendChild(pattern.canvas());

document.onmousemove = (function() {
var onmousestop = function() {
var pattern = Trianglify({
variance: 0.05
});
pattern.canvas()
},
thread;

return function() {
clearTimeout(thread);
thread = setTimeout(onmousestop, 100);
};
})();
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/trianglify/0.4.0/trianglify.min.js"></script>
<div id="home"></div>

最佳答案

onmousetop 函数中定义的var patternwindow.onload 中定义的原始pattern 没有影响功能。因此,它不会像您期望的那样修改现有的 Canvas 。相反,您可以尝试删除旧 Canvas 并附加新 Canvas 。您可能也需要在此处添加所有选项以使其按预期工作。 (此外,您错过了 window.onload 函数的右大括号 }。我想,这只是写问题时的输入错误。我也在您的问题中对其进行了编辑。)

    var patternOptions = {
width: window.innerWidth,
height: window.innerHeight,
cell_size: 120,
stroke_width: 1.3,
variance: 0.75,
seed: '9rqsn',
x_colors: 'Blues'
};
...........
..........
var onmousestop = function() {
patternOptions.variance = 0.05;
var pattern2 = Trianglify(patternOptions);
homecan.innerHTML = ''; //Clear the existing canvas
homecan.appendChild(pattern2.canvas()); //append the new canvas
},
thread;

完整演示:

//Do Traingle Canvas
window.onload = function() {
var patternOptions = {
width: window.innerWidth,
height: window.innerHeight,
cell_size: 120,
stroke_width: 1.3,
variance: 0.75,
seed: '9rqsn',
x_colors: 'Blues'
};
var pattern = Trianglify(patternOptions);
var homecan = document.getElementById('home');
homecan.appendChild(pattern.canvas());

document.onmousemove = (function() {
var onmousestop = function() {
patternOptions.variance = 0.05;
var pattern2 = Trianglify(patternOptions);
homecan.innerHTML = ''; //Clear the existing canvas
homecan.appendChild(pattern2.canvas()); //append the new canvas
},
thread;

return function() {
clearTimeout(thread);
thread = setTimeout(onmousestop, 100);
};
})();
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/trianglify/0.4.0/trianglify.min.js"></script>
<div id="home"></div>

关于javascript - 改变鼠标移动的方差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33883248/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com