Giter VIP home page Giter VIP logo

marhel / tixy-wasp Goto Github PK

View Code? Open in Web Editor NEW
2.0 4.0 1.0 23.4 MB

This repo re-implements https://tixy.land/ in Python, using a WebAssembly function to drive the animations. Also includes a simple way to run the same functions in the browser.

License: MIT License

Shell 1.15% Rust 0.16% Python 10.39% TypeScript 0.25% WebAssembly 48.83% JavaScript 11.19% Zig 0.21% HTML 27.19% CSS 0.52% C 0.11%

tixy-wasp's Introduction

tixy-wasp

This repo re-implements https://tixy.land/ in Python, using a WebAssembly function to drive the animations.

The main purpose is showing Wasm-integration from multiple languages, and tixy is just a so much more playful demonstration than some boring add(x, y) test.

Run locally with python

git clone https://github.com/marhel/tixy-wasp
cd tixy-wasp
python3 -m venv venv
source venv/Scripts/activate # for bash on Windows, .bat and .ps1 versions in the same folder
source venv/bin/activate # On macOS/Linux
pip3 install -r requirements.txt
python3 main.py

The following keys can be used in the application:

  • enter - reset time
  • spacebar - switch to a different color
  • escape - exit

tixy wasm animation

Run locally in the browser

git clone https://github.com/marhel/tixy-wasp
cd tixy-wasp
python3 -m http.server 8080

Then open http://localhost:8080 in a browser. This version does not show any performance measurements due to the fact that the timespans involved are very small (on the order of just one or two milliseconds to generate the grid once), and the timer resolution in browsers is currently intentionally unreliable in this range due to Spectre mitigations.

screenshot of tixy wasm animations in browser window

The tixy function

The required tixy function has the signature float int int int -> float which is float tixy(float t, int i, int x, int y) in C syntax.

  • t is the time since start in fractional seconds, so t is unsurprisingly 2.4 after 2.4 seconds.
  • i is the index (one more for each dot) so for a 32 x 32 grid, i is 0-1023.
  • x and y are the coordinates of the dot.

The return value determines the look of the dot at the given coordinates. A negative value is colored, a positive value is white. The magnitude determines the size of the dot (capped at -1.0 to 1.0).

So -1.0 is a big colored dot. 1.0 is a big white dot. -0.2 is a small colored dot. Zero is "black" (due to zero radius). 14.2 is the same size and color as 1.0 due to capping.

Go to https://tixy.land to see some example functions, and it's possibly quicker to experiment there as well.

Add another language

Look at https://github.com/appcypher/awesome-wasm-langs or other resources to find a language that can compile to WASM. Implement the tixy function in this language.

Add a script lang/some_lang/compile.sh that does whatever it takes to compile your tixy function producing a lang/some_lang/tixy.wasm file with this function exported. See the pre-existing implementations under the lang folder.

Then edit main.py and add a TixyHandler for your wasm file, and replace one of the pre-existing duplicate renderings with your own.

# this assumes that your .wasm file exports a "tixy" function with the proper signature
some_lang = TixyHandler('some_lang', 'langs/some_lang/tixy.wasm')
...
screen.blit(render(some_lang), square[3])

To include in the JavaScript version, edit main.js and add your language and wasm file:

var some_lang = await CreateTixyHandler('some_lang', 'langs/some_lang/tixy.wasm')
...
ctx.drawImage(render(some_lang), ... square[3])

Then submit this as a PR, including the tixy.wasm-file to help those that haven't yet installed the (wasm-)toolchain for your language.

tixy-wasp's People

Contributors

cederstrom avatar marhel avatar qerub avatar sebastian-porling avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

qerub

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.