简体   繁体   English

HTML5 Canvas线条图

[英]HTML5 Canvas line drawing

I'm working on a program that sets variables to random numbers, and then creates a line across the canvas where the y coordinates are different each time. 我正在开发一个程序,该程序将变量设置为随机数,然后在画布上创建一条线,每次y坐标都不同。 In the program, one button generates a bumpy line, and the other uses some code that makes a much smoother line. 在程序中,一个按钮产生颠簸的线条,另一个按钮使用一些使线条更加平滑的代码。

Right now, I'm working on a way to fill the space below the randomly generated line so that it will resemble a hill, but I'm running into a few problems. 现在,我正在研究一种方法来填充随机生成的线下方的空间,以使其类似于小山,但是我遇到了一些问题。

In the "text/javascript" area, I define the canvas and its coordinates, and all of the random y value variables (but they aren't used yet). 在“文本/ javascript”区域中,我定义了画布及其坐标以及所有随机y值变量(但尚未使用)。 If the "bumpy" button is clicked, it runs a function that sets the y coordinate variables to anything inside the canvas, and then draws the line. 如果单击“凹凸”按钮,它将运行一个函数,将y坐标变量设置为画布内的任何内容,然后绘制线条。 If the "Smooth" button is clicked, the code randomly sets the first y coordinate variable, and then sets the next variables to a coordinate either a little bit bigger, or a little bit smaller then the previous point, and then draws the line. 如果单击“平滑”按钮,代码将随机设置第一个y坐标变量,然后将下一个变量设置为一个坐标,该坐标比上一个点大一点或小一点,然后画线。 The third button is called "fill." 第三个按钮称为“填充”。 This button runs another function which checks to see if a hill has been drawn yet, and the begins drawing another line using all the y coordinate values +10. 该按钮运行另一个功能,该功能检查是否已绘制山丘,然后开始使用所有y坐标值+10绘制另一条线。 (In other words, draws a line directly beneath the original line.) My plan was for the button to draw lines like this all the way to the bottom of the canvas, thus filling the space beneath the line. (换句话说,在原始线的下面直接画一条线。)我的计划是让按钮在画布的底部一直画这样的线,从而填充线下的空间。 This is where it gets weird, and I think the problem is that the "fill" button uses information (stored in variables) that was created in other functions. 这是很奇怪的地方,我认为问题是“填充”按钮使用了在其他函数中创建的信息(存储在变量中)。

What should I do? 我该怎么办?

Thanks in advance. 提前致谢。

You just want to create your entire path whichever way you please but don't bother calling ctx.stroke() or ctx.fill(), then add two more line segments: One to the bottom right, then one to the bottom left. 您只想按照自己喜欢的方式创建整个路径,但是不必打扰ctx.stroke() or ctx.fill(),然后再添加两个线段:一个在右下角,然后一个在左下角。

So then you've just drawn three sides to a box. 因此,您刚刚在框上绘制了三个面。 The top, right, and bottom. 顶部,右侧和底部。 The top is actually a ton of line segments (or curves) but that doesn't matter, the idea is the same. 顶部实际上是大量的线段(或曲线),但这没关系,想法是相同的。 Then you call ctx.fill() and it will fill in that box. 然后调用ctx.fill() ,它将填充在该框中。

(You don't have to draw the left side since you're just filling. If you're stroking you'll want to call ctx.close () first) (由于填充而不必画左侧。如果要抚摸,则首先要调用ctx.close()。)

So you call ctx.fill() once at the end and that entire bottom space will be filled 因此,您最后一次调用ctx.fill(),整个底部空间将被填充

Here's a live example: http://jsfiddle.net/wKVRR/ 这是一个实时示例: http : //jsfiddle.net/wKVRR/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM