在 F# 寓言中使用 p5.js
Using p5.js with F# Fable
我正在尝试使用 F# Fable 和 p5.js 创建网页。我正确声明了 setup
和 draw
函数,并且在 HTML 中引用了 p5.js。但是似乎没有任何效果:永远不会调用 setup
函数,因此永远不会创建 canvas
。没有显示错误消息。
以下是我使用的代码。
<!doctype html>
<html>
<head>
<style>
body {
padding: 20px;
margin: 0 auto;
}
canvas {
vertical-align: top;
}
div {
margin-bottom: 20px;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/p5.js"></script>
<title>Simple Fable App</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="fable.ico" />
</head>
<body>
</body>
</html>
open Fable.Core
open Fable.Core.JsInterop
open Fable.Import
[<Emit("fill([=12=])")>]
let fill (_: int): unit = jsNative
[<Emit("rect([=12=], , , )")>]
let rect (_: int) (_: int) (_: int) (_: int): unit = jsNative
[<Emit("frameRate([=12=])")>]
let frameRate (_: int): unit = jsNative
[<Emit("createCanvas([=12=], )")>]
let createCanvas (_: int) (_: int): unit = jsNative
let setup () =
createCanvas 500 500
frameRate 15
let draw () =
fill 255
rect 10 10 100 101
如果我使用与上面完全相同的 HTML 和等效的 JS,它将按预期工作,所以我假设 Fable 搞砸了我的某个地方。
<!doctype html>
<html>
<head>
<style>
body {
padding: 20px;
margin: 0 auto;
}
canvas {
vertical-align: top;
}
div {
margin-bottom: 20px;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/p5.js"></script>
<title>Simple Fable App</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="fable.ico" />
</head>
<body>
</body>
<script>
function setup () {
createCanvas(500, 500)
frameRate(15)
}
function draw () {
fill(255)
rect(10, 10, 100, 101)
}
</script>
</html>
根据@FyodorSoikin的提示,我搜索了文档,发现了how to add properties to JS objects。
所以我将两个相关函数添加到 Browser.window
对象(即使它们成为全局函数):
Browser.window?setup <- setup
Browser.window?draw <- draw
现在一切正常。
我正在尝试使用 F# Fable 和 p5.js 创建网页。我正确声明了 setup
和 draw
函数,并且在 HTML 中引用了 p5.js。但是似乎没有任何效果:永远不会调用 setup
函数,因此永远不会创建 canvas
。没有显示错误消息。
以下是我使用的代码。
<!doctype html>
<html>
<head>
<style>
body {
padding: 20px;
margin: 0 auto;
}
canvas {
vertical-align: top;
}
div {
margin-bottom: 20px;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/p5.js"></script>
<title>Simple Fable App</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="fable.ico" />
</head>
<body>
</body>
</html>
open Fable.Core
open Fable.Core.JsInterop
open Fable.Import
[<Emit("fill([=12=])")>]
let fill (_: int): unit = jsNative
[<Emit("rect([=12=], , , )")>]
let rect (_: int) (_: int) (_: int) (_: int): unit = jsNative
[<Emit("frameRate([=12=])")>]
let frameRate (_: int): unit = jsNative
[<Emit("createCanvas([=12=], )")>]
let createCanvas (_: int) (_: int): unit = jsNative
let setup () =
createCanvas 500 500
frameRate 15
let draw () =
fill 255
rect 10 10 100 101
如果我使用与上面完全相同的 HTML 和等效的 JS,它将按预期工作,所以我假设 Fable 搞砸了我的某个地方。
<!doctype html>
<html>
<head>
<style>
body {
padding: 20px;
margin: 0 auto;
}
canvas {
vertical-align: top;
}
div {
margin-bottom: 20px;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/p5.js"></script>
<title>Simple Fable App</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="fable.ico" />
</head>
<body>
</body>
<script>
function setup () {
createCanvas(500, 500)
frameRate(15)
}
function draw () {
fill(255)
rect(10, 10, 100, 101)
}
</script>
</html>
根据@FyodorSoikin的提示,我搜索了文档,发现了how to add properties to JS objects。
所以我将两个相关函数添加到 Browser.window
对象(即使它们成为全局函数):
Browser.window?setup <- setup
Browser.window?draw <- draw
现在一切正常。