如何将交互和动画添加到处理中绘制的形状?
我正在尝试编码装满形状(房屋)的画布,并在处理中对它们进行动画。
这是一个形状的示例:
void house(int x, int y) {
pushMatrix();
translate(x, y);
fill(0, 200, 0);
triangle(15, 0, 0, 15, 30, 15);
rect(0, 15, 30, 30);
rect(12, 30, 10, 15);
popMatrix();
}
通过动画,我的意思是将它们移动到随机的方向上。
我还想添加基本的互动:当悬停在房屋上时,颜色会改变。
目前,我设法渲染了一个装满房屋的画布:
void setup() {
size(500, 500);
background(#74F5E9);
for (int i = 30; i < 500; i = i + 100) {
for (int j = 30; j < 500; j = j + 100) {
house(i, j);
}
}
}
void house(int x, int y) {
pushMatrix();
translate(x, y);
fill(0, 200, 0);
triangle(15, 0, 0, 15, 30, 15);
rect(0, 15, 30, 30);
rect(12, 30, 10, 15);
popMatrix();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有看到源代码:您尝试的草图很难说。
它们可以通过多种方式进行动画,目前尚不清楚您的意思。例如,是每个正方形的位置/旋转/刻度,是每个正方形的角落/顶点吗?
您的脑海中可能有一个清晰的想法,但是问题的当前形式是模棱两可的。我们也不知道您的舒适度具有各种概念,例如类/对象/pvector/pshape/etc。如果您要“故事板”这个动画,它会是什么样?将问题分解并以任何人都能理解的方式解释它实际上也可以帮助您自己找出解决方案。
处理有很多例子。根据我对您的问题的理解,我发现一些相关的内容。
您可以查看对象并创建形状示例:
pShape
参考的顶点,我只需复制/粘贴上面提到的示例:
objects
polygonpshapeoop3 :
wigglepshape:
update
基于您的评论,这是修改了草图的版本,因此盘旋房屋的颜色更改:
代码为已评论,但这是主要的要点:
house()
函数已更改,因此您可以指定颜色Overcret()
函数已从翻转示例Overhouse()
函数使用OVERTRECT()
,但添加了水平偏移以考虑到房屋是从中间点绘制的(房屋提示是形状的枢轴点)关于动画,处理具有示例:
让我们开始以正弦运动为例。
sin()
函数呈一个角度(默认情况下,在Radians中)并返回-1.0至1.0之间的值,因为您已经在2D网格中计算每个房屋的位置,因此您可以使用
sin()
来抵消每个位置以使其动画。关于它的好处是周期性:无论您提供哪个角度,您始终获得-1.0和1.0之间的值。这将为您节省需要将每个房屋的当前X,Y位置存储在阵列中,以便您可以将它们递增为不同的方向。这是上述草图的修改版本,该版本使用
sin()
进行动画:仔细阅读评论并尝试调整代码以更好地了解其工作原理,并获得不同的动画的乐趣。
主要更改是:
house()
函数使用float x,y位置(而不是int):这是为了避免转换float
int
int < /code>使用
sin()
,map()
并获取更平滑的动作(而不是运动将“捕捉”到整个像素的运动)计算X偏移量计算为可重复使用的功能的3个指令包装动画,您可以进行进一步的实验。如果您使用了类似技术,则每个房屋的Y位置怎么办? x和y呢?
逐步浏览代码。尝试理解它,更改它,打破它,修复并制作新的草图重复使用代码。
Without seeing source code: your attempted sketch it's very hard to tell.
They can be animated in many ways and it's unclear what you mean. For example, is that the position/rotation/scale of each square, is it the corners/vertices of each square, both ?
You might have a clear idea in your mind, but the current form of the question is ambiguous. We also don't know you're comfort level with various notions such as classes/objects/PVector/PShape/etc. If you were to 'story board' this animation what would it look like ? Breaking the problem down and explaining it in a way that anyone can understand might actually help you figure out a solution on your own as well.
Processing has plenty of examples. Here are a few I find relevant based on what my understanding is of your problem.
You can have a look at the Objects and Create Shapes examples:
PShape
For reference I'm simply copy/pasting the examples mentioned above here as well:
Objects
PolygonPShapeOOP3:
WigglePShape:
Update
Based on your comments here's an version of your sketch modified so the color of the hovered house changes:
The code is commented, but here are the main takeaways:
house()
function has been changed so you can specify a coloroverRect()
function has been copied from the Rollover exampleoverHouse()
function usesoverRect()
, but adds a horizontal offset to take into account the house is drawn from the middle top point (the house tip is the shape's pivot point)Regarding animation, Processing has tons of examples:
Let's start take sine motion as an example.
The
sin()
function takes an angle (in radians by default) and returns a value between -1.0 and 1.0Since you're already calculating positions for each house within a 2D grid, you can offset each position using
sin()
to animate it. The nice thing about it is cyclical: no matter what angle you provide you always get values between -1.0 and 1.0. This would save you the trouble of needing to store the current x, y positions of each house in arrays so you can increment them in a different directions.Here's a modified version of the above sketch that uses
sin()
to animate:Read through the comments and try to tweak the code to get a better understanding of how it works and have fun coming up with different animations.
The main changes are:
house()
function to use float x,y positions (instead of int): this is to avoid convertingfloat
toint
when usingsin()
,map()
and get smoother motions (instead of motion that "snaps" to whole pixels)Wrapping the 3 instructions that calculate the x offset into a reusable function would allow you do further experiment. What if you used a similar technique the y position of each house ? What about both x and y ?
Go through the code step by step. Try to understand it, change it, break it, fix it and make new sketches reusing code.