我有一个CircleMorph
,我想扩大它的直径,可能是用它创建一个小动画。
b := CircleMorph new.
b color: Color transparent.
b borderWidth: 2.
b extent:100 @ 100.
b openInWorld.
如果我使用循环或step
方法来执行此操作会更好吗?如果您推荐该step
方法,那么我该怎么做?
你可以创建一个CircleMorph
被调用的子类GrowingCircleMorph
。然后实施step
:
step
self extent: (self extent) + 1.
(self extent) > (200@200) ifTrue: [ self stopStepping ]
现在,如果您在 World 中打开新的 GrowingCircleMorph 实例,它将开始增长到 201@201。
要更改速度,请实施stepTime
并返回步骤之间的所需时间(以毫秒为单位)。
更新:如果您希望中心保持不变,请更改圆形变形的边界,而不是范围:
step
self bounds: ((self bounds) expandBy: 1).
(self extent) > (200@200) ifTrue: [ self stopStepping ]
如果你想玩这些东西,让它通用:
HOCanvasMorph>>drawOn: aCanvas
super drawOn: aCanvas.
drawBlock ifNotNil: [
aCanvas
translateBy: self bounds origin
clippingTo: self bounds
during: [:canvas | drawBlock value: canvas]]
HOCanvasMorph>>drawBlock: aBlock
drawBlock := aBlock.
self changed.
HOCanvasMorph class>>example
|m|
m := HOCanvasMorph new openInWorld.
"to draw:"
m drawBlock: [:c |
c line: 10@10 to: 100@100 color: Color red
]
HOCanvasMorph class>>napperons
"Some mathematical fun"
| m |
m := HOCanvasMorph new openInWorld. "to draw:"
m
drawBlock: [ :aCanvas |
| n r t xa xc xb yc ya yb longueur nombreDeDroites |
longueur := 150.
nombreDeDroites := 30. "super drawOn: aCanvas."
xc := 200.
yc := 200.
n := 15.
r := 100.
0 to: n - 2 do: [ :i |
t := 2 * Float pi * i / n.
i + 1 to: n - 1 do: [ :j |
| tj |
tj := 2 * Float pi * j / n.
xa := xc + (r * t cos).
ya := yc + (r * t sin).
xb := xc + (r * tj cos).
yb := yc + (r * tj sin).
(aCanvas asBalloonCanvas) line: xa @ ya to: xb @ yb color: Color green ] ] ]
由于块可以使用任何更改,因此将这些更改放在一个 step 方法中。