ZIM JavaScript Canvas Framework

repository·master·Indexed 20 days ago

https://github.com/danzen/zimjs

A JavaScript Canvas Framework for creative coding, games, apps, and interactive media. ZIM provides a high-level API for rapid visual development on desktop and mobile, with support for vanilla JavaScript, TypeScript, and frameworks including Vue, Svelte, React, and Angular. It includes specialized helper modules for physics, game utilities, Three.js integration, socket networking, camera controls, and visual effects.

Tokens
3.1K
Snippets
13
Records
16
Agent score
70%

What's inside ZIMjs

  1. Install ZIM via NPM with Vite

    master

    To set up a ZIM project using NPM and Vite for vanilla JavaScript or TypeScript, follow these steps:

    1. Initialize Project: Run npm create vite in your terminal and follow the prompts (e.g., select Vanilla and JavaScript or TypeScript).
    2. Navigate to Project: cd yourProject
    3. Install ZIM: npm i zimjs
    4. Start Development Server: npm run dev
    5. Build for Production: npm run build (this generates a dist/ folder with minified code).
    npm create vite
    cd yourProject
    npm i zimjs
    npm run dev
  2. Use ZIM with Vue

    master

    To use ZIM in a Vue application, you can either use the zim namespace or use zim.zimplify() to make ZIM commands global. It is recommended to initialize the zim.Frame within the onMounted lifecycle hook and call frame.dispose() within onBeforeUnmount to prevent memory leaks.

    With zim namespace

    Import zim from zimjs and access classes via new zim.Frame() and new zim.Circle().

    Without zim namespace

    Call zim.zimplify() to make ZIM commands global, allowing you to use new Frame() and new Circle() directly.

    // With zim namespace
    <script setup>  
      import { onMounted, onBeforeUnmount } from "vue";
      import zim from "zimjs";
    
      let frame;
      onMounted(() => {
        frame = new zim.Frame({
          scaling: "zim",
          width: 500,
          height: 400,
          color: light,
          ready: () => {
              new zim.Circle(50, red).center().drag();
          }
        });
      });
    
      onBeforeUnmount(() => {
        frame.dispose();
      });  
    </script>
    
    <template>
      <div id="zim"></div>
    </template>
  3. Use ZIM with Angular

    master

    In Angular, ZIM is used with TypeScript.

    1. Import Frame and other components directly from zimjs.
    2. Initialize the frame in the ngAfterContentInit lifecycle hook.
    3. Call this.frame?.dispose() in the ngOnDestroy hook to clean up resources.
    import { AfterContentInit, Component, OnDestroy } from '@angular/core';
    import { Frame, Circle } from 'zimjs';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent implements OnDestroy, AfterContentInit {
      frame: Frame | undefined;
    
      ngOnDestroy(): void {
        this.frame?.dispose();
      }
    
      ngAfterContentInit(): void {
        this.frame = new Frame({
          scaling: FIT,
          width: 600,
          height: 300,
          ready: () => {
            new Circle(50, red).center().drag();
          },
        });
      }
    
      title = 'ZIM in Angular';
    }
  4. Use ZIM with React

    master

    To use ZIM in React, create a component that manages the zim.Frame instance.

    1. Use componentDidMount to instantiate the new zim.Frame().
    2. Use componentWillUnmount to call this.frame?.dispose().
    3. Important: If using StrictMode, ensure the ZIM container div is inside StrictMode, but include the <ZimFrame /> component outside of it to avoid double-initialization issues during development.

    You can use the zim namespace or call zim.zimplify() to use global commands.

    // React with zim namespace
    import { Component, ReactNode, StrictMode } from "react";
    import zim from "zimjs";
    
    class ZimFrame extends Component {
      frame: zim.Frame | undefined;
    
      componentDidMount(): void {
          this.frame = new zim.Frame({
            scaling: "zim",
            width: 500,
            height: 400,
            color: light,
            ready: () => {
                new zim.Circle(50, red).center().drag();
            }
          });
      }
      componentWillUnmount(): void {
          this.frame?.dispose();
      }
      render(): ReactNode {
          return null;
      }
    }
    
    function App() {
      return (
          <>
          <div>
              <StrictMode>
              <div id='zim'></div>
              </StrictMode>
              <ZimFrame />
          </div>
          </>
      )
    }
    export default App;
  5. Install @types/soundjs for TypeScript support

    master

    To use SoundJS with TypeScript, install the type definitions via npm. This package provides type definitions for SoundJS 0.6.0.

    Dependencies:

    • createjs-lib
    • preloadjs

    Note on usage: The definitions are structured for a global environment where the createjs namespace is available.

    npm install --save @types/soundjs
  6. Quick Start without NPM (CDN/Template)

    master

    For the fastest way to start coding without a build tool or NPM, you can use ZIM via CDN or a simple HTML template:

    1. Go to zimjs.com/code.
    2. Click the copy button.
    3. Paste the code into an empty .html file.
    4. Open the file in any web browser.

    Alternatively, you can use the ZIM Editor for an online development experience.

  7. Use ZIM with Svelte

    master

    When integrating ZIM with Svelte, initialize the zim.Frame inside the onMount lifecycle hook and ensure you call frame.dispose() in the onDestroy hook.

    • TypeScript: Define the frame type (e.g., let frame: Frame;) and import zim.
    • JavaScript: Use zim.zimplify() to enable global commands like new Frame() and new Circle() without the namespace prefix.
    // Svelte with zim namespace and TypeScript
    <script lang="ts">  
      import { onMount, onDestroy } from "svelte";
      import zim from "zimjs";
    
      let frame: Frame;
      onMount(() => {
        frame = new zim.Frame({
          scaling: "zim",
          width: 500,
          height: 400,
          color: light,
          ready: () => {
              new zim.Circle(50, red).center().drag();
          }
        });
      });
    
      onDestroy(() => {
        frame.dispose();
      });
    </script>
    
    <main>
      <div id="zim"></div>
    </main>
  8. Configure ZIM for TypeScript

    master

    For TypeScript projects, import specific classes from zimjs instead of using zimplify(). Replace the contents of src/main.ts with the following template:

    // ZIM - JavaScript Canvas Framework - https://zimjs.com - code creativity
    import {Frame, Circle} from "zimjs";
    
    // See Docs under Frame for FIT, FILL, FULL, and TAG
    new Frame(FIT, 1024, 768, light, dark, ready);
    
    function ready() {
        // given F (Frame), S (Stage), W (width), H (height)
        // put code here
        
        new Circle(100, purple)
            .center()
            .drag();
            
    } // end ready