@2dimensions/flare-js 中文文档教程

发布于 5年前 浏览 23 项目主页 更新于 3年前

Flare-JS

带有 Canvas 渲染的 Javascript ES6 运行时。

CanvasKit vs Canvas

目前有两个分支:使用 CanvasKit 渲染的 master(通过 WebAssembly 的 Skia)和使用标准 CanvasRenderingContext2D 的 context2d。 context2d 分支已弃用。

Building

使用 NPM 获取依赖项,然后使用 webpack 构建:

npm install
npm start -- --watch

Flare-JS Example

有几个步骤可以开始渲染 Flare 文件:

  1. Create a canvas element in HTML
  2. Instantiate and initialize the Graphics object with the canvas reference
  3. Start the render loop with window.requestAnimationFrame()
  4. Load the an Actor from file with Flare.ActorLoader.load(fileLocation, callback)
  5. Initialize and instantiate the Actor - that will initialize the ActorArtboards for the loaded Flare file:
    • First Actor.initialize()
    • then actor.makeInstance()
  6. Create a new AnimationInstance(actor, animation)
  7. Advance the AnimationInstance within the window.requestAnimationFrame callback:
    • increase its time value with the provided setter animationInstance.time
    • then apply it with animationInstance.apply()
  8. Advance the actor: actor.advance(elapsed)

Running the Example

example 文件夹 包含两个文件,example.htmlexample.js,以及有关如何使用此运行时的示例实现。

使用NPM获取gl-matrix依赖:

npm install gl-matrix

安装完成后,从node_modules/gl-matrix-dist复制gl-matrix.js进入 repo 的 /build 文件夹。 (注意 canvaskit 与此 repo 一起下载,并且已经存在于 /build 文件夹中。)

此时运行 webpack 构建:

npm run dev

使用本地网络服务器,例如 NGINXMAMP 公开资源。

example.html

该文件包含渲染 Flare 文件所需的 canvas 元素。 它还将加载必要的依赖项(即 Flare-JSgl-matrixcanvaskit)和示例运行脚本 example .js。

example.js

js 文件创建一个 FlareExample 对象。 它的构造函数初始化了 Graphics 对象,启动渲染循环并回调脚本。

HTML 文件中的回调将调用 FlareExample.load(filename) 从磁盘加载文件。 一旦 Flare.ActorLoader 完成,它的加载回调将通过 FlareExample.setActor() 完成 Example 中的 actor 设置。

FlareExample.setActor 将执行画板和动画的初始化和实例化。

渲染循环 - 即 FlareExample.advance() 现在可以推进 Actor、动画,最后通过 FlareExample.Draw() 绘制到画布。

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request.

License

有关许可权利和限制 (MIT),请参阅 LICENSE 文件。

Flare-JS

Javascript ES6 runtime with Canvas rendering.

CanvasKit vs Canvas

There are currently two branches: master with CanvasKit rendering (Skia via WebAssembly) and context2d with the standard CanvasRenderingContext2D. The context2d branch is deprecated.

Building

Use NPM to get the dependencies and then build with webpack:

npm install
npm start -- --watch

Flare-JS Example

There are a few steps to start rendering a Flare file:

  1. Create a canvas element in HTML
  2. Instantiate and initialize the Graphics object with the canvas reference
  3. Start the render loop with window.requestAnimationFrame()
  4. Load the an Actor from file with Flare.ActorLoader.load(fileLocation, callback)
  5. Initialize and instantiate the Actor - that will initialize the ActorArtboards for the loaded Flare file:
    • First Actor.initialize()
    • then actor.makeInstance()
  6. Create a new AnimationInstance(actor, animation)
  7. Advance the AnimationInstance within the window.requestAnimationFrame callback:
    • increase its time value with the provided setter animationInstance.time
    • then apply it with animationInstance.apply()
  8. Advance the actor: actor.advance(elapsed)

Running the Example

The example folder contains two files, example.html and example.js, with an example implementation on how to use this runtime.

Use NPM to get the gl-matrix dependency:

npm install gl-matrix

After the installation completes, copy gl-matrix.js from node_modules/gl-matrix-dist into the repo's /build folder. (N.B. canvaskit is downloaded with this repo, and already present in the /build folder.)

At this point run a webpack build:

npm run dev

Use a local web server such as NGINX or MAMP to expose the resources.

example.html

This file contains the canvas element that we need to render the Flare file. It'll also load the necessary dependencies (i.e. Flare-JS, gl-matrix, canvaskit) and the example running script example.js.

The <script> tag will have implement the onLoad() callback for the body HTML Element: a FlareExample object is initialized with the canvas reference, and passed a callback function that'll load the Flare file from disk - flareExample.load(filename, callback).

example.js

This js file creates a FlareExample object. Its constructor initialized the Graphics object, starts the rendering loop and calls-back to the script.

The callback, in the HTML file will call FlareExample.load(filename) to load the file from disk. Once Flare.ActorLoader is done, its load callback will finish setting up the actor within the Example via FlareExample.setActor().

FlareExample.setActor will perform the initialization and instantiation of the Artboards, and the Animation.

The render loop - that is FlareExample.advance() can now advance the Actor, the Animation and lastly draw to canvas via FlareExample.Draw().

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request.

License

See the LICENSE file for license rights and limitations (MIT).

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文