【发布时间】:2019-06-07 10:49:32
【问题描述】:
我需要做一些事情,但我的数学能力达不到标准。我想要构建的是类似于this demo, 的东西,但我需要它是圆形和多边形的混合体,而不是一条线,可以这么说。黑线应该是动态的,随机生成的,基本上充当页面的边框。
目前,我正在剖析 this answer,希望能够将其转化为这个,但我非常怀疑我能否解决这个问题。
知道如何做到这一点,或者任何人都可以解释数学吗?
以下是我对上面链接的答案中的代码的注释。
var
cw = cvs.width = window.innerWidth,
ch = cvs.height = window.innerHeight,
cx = cw / 2,
cy = ch / 2,
xs = Array(),
ys = Array(),
npts = 20,
amplitude = 87, // can be val from 1 to 100
frequency = -2, // can be val from -10 to 1 in steps of 0.1
ctx.lineWidth = 4
// creates array of coordinates that
// divides page into regular portions
// creates array of weights
for (var i = 0; i < npts; i++) {
xs[i] = (cw/npts)*i
ys[i] = 2.0*(Math.random()-0.5)*amplitude
}
function Draw() {
ctx.clearRect(0, 0, cw, ch);
ctx.beginPath();
for (let x = 0; x < cw; x++) {
y = 0.0
wsum = 0.0
for (let i = -5; i <= 5; i++) {
xx = x; // 0 / 1 / 2 / to value of screen width
// creates sequential sets from [-5 to 5] to [15 to 25]
ii = Math.round(x/xs[1]) + i
// `xx` is a sliding range with the total value equal to client width
// keeps `ii` within range of 0 to 20
if (ii < 0) {
xx += cw
ii += npts
}
if (ii >= npts){
xx -= cw
ii -= npts
}
// selects eleven sequential array items
// which are portions of the screen width and height
// to create staggered inclines in increments of those portions
w = Math.abs(xs[ii] - xx)
// creates irregular arcs
// based on the inclining values
w = Math.pow(w, frequency)
// also creates irregular arcs therefrom
y += w*ys[ii];
// creates sets of inclining values
wsum += w;
}
// provides a relative position or weight
// for each y-coordinate in the total path
y /= wsum;
//y = Math.sin(x * frequency) * amplitude;
ctx.lineTo(x, y+cy);
}
ctx.stroke();
}
Draw();
【问题讨论】:
-
对形状有什么限制吗?考虑到波浪形应该大致遵循的路径吗?
-
@LutzL 它应该是一个矩形的粗略近似值(或者屏幕的
clientWidth恰好是什么)。它应该几乎就像页面周围的边框。它应该代表电子游戏中的战争迷雾。但我想让波浪线动态化,并且能够调整绿色部分的厚度或者换句话说缩小“矩形”可见区域也会很好。 -
@LutzL 有点像边框,但沿内边缘呈波浪状,而不是直线。 kind of like this.,但可能有点波动。我计划使波浪动态化,并且我计划使其外边缘周围不透明,然后越靠近中心逐渐变透明。我要睡觉了,但我会回来看看
标签: javascript html math canvas dynamic