Coder Social home page Coder Social logo

d12frosted / terminal-canvas Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ghaiklor/terminal-canvas

0.0 1.0 0.0 1.6 MB

Manipulate the cursor in your terminal via high-performant, low-level, canvas-like API

Home Page: https://ghaiklor.github.io/terminal-canvas/

License: MIT License

JavaScript 3.61% TypeScript 96.39%

terminal-canvas's Introduction

terminal-canvas

Travis (.com) Codecov npm

GitHub Follow Twitter Follow

Manipulate the cursor in your terminal via high-performant, low-level, canvas-like API.

Entirely written with TypeScript, terminal-canvas exposes features that you can use for rendering in terminal. High-performance algorithms and optimizations allow to render only cells which have been changed. Just look at the demo videos below to see, what you can do with terminal-canvas ๐Ÿ˜ƒ

Demo

Touhou - Bad Apple (ASCII Art) Touhou - Bad Apple (Colorful Edition) Doom (Colorful Edition)
1 1 1

Getting Started

Install it via npm:

npm install terminal-canvas

Include in your project:

const Canvas = require('terminal-canvas');
const canvas = new Canvas();

canvas.moveTo(10, 10).write('Hello, world').flush();

Examples

A lot of examples are available to you here

API Reference

API Reference is accessible here

How terminal-canvas works

Control Sequences

terminal-canvas uses VT100 compatible control sequences to manipulate the cursor in the terminal.

You can find a lot of useful information about that here:

The next thing which terminal-canvas does is wrap those control codes, so you are able to call JavaScript methods.

Virtual Terminal

The first releases of the terminal-canvas (which was kittik-cursor) were based on real-time updating terminal cursor, when you are calling some method on the canvas.

This caused performance issues. So I decided to create my own wrapper around terminal cells.

Each real cell in the terminal has a wrapper (class Cell in the src folder). The main problem, which Cell resolves, is to render each cell independently from another cells. So I can grab any cell at any coordinate and render it independently from others.

It works the following way. Each Cell has style settings and position in the real terminal. When you are converting Cell to the control sequences, it concatenates the following sequences:

  1. Convert cell position to control sequence
  2. Convert foreground and background color to control sequence
  3. Convert display settings to control sequences
  4. Pre-pend the cell char with sequences above
  5. Reset all display settings to default

That way, each cell wrapped in own control sequences that can be flushed at any moment.

Difference between two frames

The last thing I did, was update only cells that really changed.

The algorithm is simple.

When you are writing to the canvas, all write operations mark virtual cells as modified cells. After some time, you decide to flush changes. When flush() method is called it does 2 things:

  1. Iterate through all cells and find only cells with modified marker;
  2. Convert modified cell to control sequence and compare that sequence with the sequence that was used at the previous frame;
  3. If they are not equal, store new control sequence and write to stream, otherwise, ignore it

That's how I made it possible to render videos in the terminal at 30 FPS.

BTW, if I remove Throttle stream, I'm getting 120 FPS ๐Ÿ˜ƒ

License

MIT

terminal-canvas's People

Contributors

d12frosted avatar dependabot[bot] avatar ghaiklor avatar greenkeeperio-bot avatar

Watchers

 avatar

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.