Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
硅谷海盗
kotlin
提交
f07523df
K
kotlin
项目概览
硅谷海盗
/
kotlin
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
K
kotlin
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
f07523df
编写于
5月 31, 2012
作者:
P
pTalanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
"Hello, Kotlin" example working with the new library
上级
527c4585
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
85 addition
and
66 deletion
+85
-66
js/js.translator/testFiles/webDemoCanvasExamples/cases/Hello, Kotlin.kt
...or/testFiles/webDemoCanvasExamples/cases/Hello, Kotlin.kt
+85
-66
未找到文件。
js/js.translator/testFiles/webDemoCanvasExamples/cases/Hello, Kotlin.kt
浏览文件 @
f07523df
/*
This
example is just simple text floating around. If u are using chrome, there is a bug that spoil the visuals
.
This
shows simple text floating around
.
*/
package
hello
import
js.*
import
html5.*
import
jquery.*
import
js.
dom.html5.
*
import
js.dom.html.window
import
j
s.j
query.*
val
context
=
getContext
()
val
height
=
getCanvas
().
height
val
width
=
getCanvas
().
width
val
canvas
:
HTMLCanvasElement
get
()
{
return
window
.
document
.
getElementsByTagName
(
"canvas"
).
item
(
0
)
!!
as
HTMLCanvasElement
}
// class representing a floating
text
class
HelloKotlin
()
{
var
relX
=
0.2
+
0.2
*
Math
.
random
()
var
relY
=
0.4
+
0.2
*
Math
.
random
()
val
context
:
CanvasCon
text
get
()
{
return
canvas
.
getContext
(
"2d"
)
!!
}
val
absX
:
Double
get
()
=
(
relX
*
width
)
val
absY
:
Double
get
()
=
(
relY
*
height
)
var
relXVelocity
=
randomVelocity
()
var
relYVelocity
=
randomVelocity
()
val
width
:
Double
get
()
{
return
canvas
.
width
}
val
height
:
Double
get
()
{
return
canvas
.
height
}
val
message
=
"Hello, Kotlin!"
val
textHeightInPixels
=
20
{
context
.
font
=
"bold ${textHeightInPixels}px Georgia, serif"
}
val
textWidthInPixels
=
context
.
measureText
(
message
).
width
fun
draw
()
{
context
.
save
()
move
()
// if you using chrome chances are good you wont see the shadow
context
.
shadowColor
=
"#000000"
context
.
shadowBlur
=
5.0
context
.
shadowOffsetX
=
-
4.0
context
.
shadowOffsetY
=
4.0
context
.
fillStyle
=
"rgb(242,160,110)"
context
.
fillText
(
message
,
absX
.
toInt
(),
absY
.
toInt
())
context
.
restore
()
}
// class representing a floating text
class
HelloKotlin
()
{
var
relX
=
0.2
+
0.2
*
Math
.
random
()
var
relY
=
0.4
+
0.2
*
Math
.
random
()
val
absX
:
Double
get
()
=
(
relX
*
width
)
val
absY
:
Double
get
()
=
(
relY
*
height
)
var
relXVelocity
=
randomVelocity
()
var
relYVelocity
=
randomVelocity
()
fun
move
()
{
val
relTextWidth
=
textWidthInPixels
/
width
if
(
relX
>
(
1.0
-
relTextWidth
-
relXVelocity
.
abs
)
||
relX
<
relXVelocity
.
abs
)
{
relXVelocity
*=
-
1
val
message
=
"Hello, Kotlin!"
val
textHeightInPixels
=
20
{
context
.
font
=
"bold ${textHeightInPixels}px Georgia, serif"
}
val
relTextHeight
=
textHeightInPixels
/
height
if
(
relY
>
(
1.0
-
relYVelocity
.
abs
)
||
relY
<
relYVelocity
.
abs
+
relTextHeight
)
{
relYVelocity
*=
-
1
val
textWidthInPixels
=
context
.
measureText
(
message
)
!!
.
width
fun
draw
()
{
context
.
save
()
move
()
// if you using chrome chances are good you wont see the shadow
context
.
shadowColor
=
"#000000"
context
.
shadowBlur
=
5.0
context
.
shadowOffsetX
=
-
4.0
context
.
shadowOffsetY
=
4.0
context
.
fillStyle
=
"rgb(242,160,110)"
context
.
fillText
(
message
,
absX
.
toInt
(),
absY
.
toInt
())
context
.
restore
()
}
relX
+=
relXVelocity
relY
+=
relYVelocity
}
fun
randomVelocity
()
=
0.03
*
Math
.
random
()
*
(
if
(
Math
.
random
()
<
0.5
)
1
else
-
1
)
fun
move
()
{
val
relTextWidth
=
textWidthInPixels
/
width
if
(
relX
>
(
1.0
-
relTextWidth
-
relXVelocity
.
abs
)
||
relX
<
relXVelocity
.
abs
)
{
relXVelocity
*=
-
1
}
val
relTextHeight
=
textHeightInPixels
/
height
if
(
relY
>
(
1.0
-
relYVelocity
.
abs
)
||
relY
<
relYVelocity
.
abs
+
relTextHeight
)
{
relYVelocity
*=
-
1
}
relX
+=
relXVelocity
relY
+=
relYVelocity
}
fun
randomVelocity
()
=
0.03
*
Math
.
random
()
*
(
if
(
Math
.
random
()
<
0.5
)
1
else
-
1
)
val
Double
.
abs
:
Double
get
()
=
if
(
this
>
0
)
this
else
-
this
val
Double
.
abs
:
Double
get
()
=
if
(
this
>
0
)
this
else
-
this
}
fun
renderBackground
()
{
context
.
save
()
context
.
fillStyle
=
"#5C7EED"
context
.
fillRect
(
0
,
0
,
width
,
height
)
context
.
restore
()
context
.
save
()
context
.
fillStyle
=
"#5C7EED"
context
.
fillRect
(
0
,
0
,
width
,
height
)
context
.
restore
()
}
fun
main
(
args
:
Array
<
String
>)
{
val
interval
=
50
// we pass a literal that constructs a new HelloKotlin object
val
logos
=
Array
(
3
)
{
HelloKotlin
()
}
jq
{
setInterval
({
renderBackground
()
for
(
logo
in
logos
)
{
logo
.
draw
()
}
},
interval
)
}
fun
main
(
args
:
Array
<
String
>)
{
jq
{
val
interval
=
50
// we pass a literal that constructs a new HelloKotlin object
val
logos
=
Array
(
3
)
{
HelloKotlin
()
}
setInterval
({
renderBackground
()
for
(
logo
in
logos
)
{
logo
.
draw
()
}
},
interval
)
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录