# code-gif-generator

> Generate scrolling GIFs from code snippets

Latest version **1.0.3** (published 2020-09-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install code-gif-generator
pnpm add code-gif-generator
yarn add code-gif-generator
bun add code-gif-generator
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2020-09-10 |
| First published | 2020-09-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 489.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | yes |
| GitHub stars | 7 |
| Author | Laurens Deprost |
| Maintainers | laudep |
| Keywords | gif, CodeMirror, animation |

## Links

- npm: https://www.npmjs.com/package/code-gif-generator
- Repository: https://github.com/laudep/code-gif-generator
- Homepage: https://github.com/laudep/code-gif-generator#readme
- Issues: https://github.com/laudep/code-gif-generator/issues
- npm.io page: https://npm.io/package/code-gif-generator

## Dependencies (8)

- [pngjs](https://npm.io/package/pngjs.md) ^3.4.0
- [nodemon](https://npm.io/package/nodemon.md) ^2.0.4
- [winston](https://npm.io/package/winston.md) ^3.3.3
- [imagemin](https://npm.io/package/imagemin.md) ^7.0.0
- [puppeteer](https://npm.io/package/puppeteer.md) ^5.2.1
- [gif-encoder](https://npm.io/package/gif-encoder.md) ^0.7.2
- [imagemin-giflossy](https://npm.io/package/imagemin-giflossy.md) ^5.1.10
- [imagemin-gifsicle](https://npm.io/package/imagemin-gifsicle.md) ^6.0.1

## Alternatives

- [@mce/gif](https://npm.io/package/@mce/gif.md) — 2.6K weekly downloads
- [cleanse](https://npm.io/package/cleanse.md) — 173 weekly downloads
- [str](https://npm.io/package/str.md) — 127 weekly downloads
- [naming](https://npm.io/package/naming.md) — 95 weekly downloads
- [tap-telco-api](https://npm.io/package/tap-telco-api.md) — 19 weekly downloads

## Recent versions

- 1.0.3 (latest) — 2020-09-10
- 1.0.2 — 2020-09-10
- 1.0.1 — 2020-09-10
- 1.0.0 — 2020-09-10

## README

<p align="center">
  <img height="250" src="https://raw.githubusercontent.com/laudep/code-gif-generator/master/docs/img/generating.gif">
</p>
<h1 align="center"> code-gif-generator </h1>
<p align="center">
  <b>Generate scrolling GIFs from code snippets 🚀</b>
</p>

<p align='center'>
  <a href='https://www.npmjs.com/package/code-gif-generator'>
    <img src='https://img.shields.io/npm/v/code-gif-generator.svg' alt='npm version'>
  </a>
  <a href='https://codecov.io/gh/laudep/code-gif-generator'>
    <img src='https://codecov.io/gh/laudep/code-gif-generator/branch/master/graph/badge.svg' alt='code coverage'>
  </a>
  <a href='https://github.com/laudep/code-gif-generator/actions'>
    <img src='https://img.shields.io/github/workflow/status/laudep/code-gif-generator/test' alt='CI status'>
  </a>
</p>

## Description
Code-gif-generator is a tool for generating animated GIF files from code snippets.  
Use them to spice up your blog posts, documentation, or README files.  
Build on top of the CodeMirror editor, more than 150 programming languages are supported.

- [Online demo](#online-demo)
- [Installation](#installation)
- [Usage](#usage)
  * [Minimal example](#minimal-example)
    + [Code](#code)
    + [Output](#output)
  * [Gif from this README file](#gif-from-this-readme-file)
    + [Code](#code-1)
    + [Output](#output-1)
- [API](#api)
  * [generateGif](#api)
  * [Gif.save](#--gif--)
  * [Gif.getBuffer](#--gif--)
  * [Gif.getCompressedBuffer](#--gif--)
  * [Presets](#presets)
    + [default](#default)
    + [fast](#fast)
    + [smooth](#smooth)
    + [ultra](#ultra)
- [Built with](#built-with)
- [License](#license)

## Online demo

Check out the online demo:  
[codetogif.io](https://codetogif.io/)

## Installation

```sh
npm install --save code-gif-generator
```

## Usage

### Minimal example

#### Code

```js
const generateGif = require('code-gif-generator');

generateGif(`console.log('Hello World!')`).then(gif => gif.save());
```

#### Output

A single frame GIF in the current working directory:  

![Screenshot: 'Hello World!](https://raw.githubusercontent.com/laudep/code-gif-generator/master/docs/img/generating.gif)


### Gif from this README file

#### Code

```js
const generateGif = require('code-gif-generator');

const createReadmeGif = async () => {
   // get the content of the README file
   const readmeContent =  await fs.promises.readFile('../README.md', 'utf8');
   // create a GIF from the readme file
   const gif = await generateGif(readmeContent, {
    preset: 'ultra',   // scroll slowly, up to 250 frames
    mode: 'markdown',   // pass the snippet programming language
    theme: 'monokai',   // theme for the code editor
    lineNumbers: false, // hide line numbers
  });
   // save the GIF in the docs/img folder
   const gifPath = await gif.save('readme-content', path.resolve(__dirname, '../docs/img'));
   return gifPath;
}

createReadmeGif().then(gifPath => console.log(`Gif saved: ${gifPath}`));
```

#### Output

A scrolling GIF in the docs/img folder:  

<img alt="Screenshot: 'README.md'" src="https://github.com/laudep/code-gif-generator/blob/master/docs/img/readme-content.gif?raw=true" width="45%">

## API

### generateGif(`code`: string, `options`: object): *Promise‹[Gif](#gif)›*

Generate an animated GIF for a code snippet

**Parameters:**

▪ **code**: *string*

the code snippet

▪ **options**: *object*

Name | Type | Default | Description |
------ | ------ | ------ | ------ |
`mode` | string | "javascript" | code language |
`theme` | string | "material-darker" | code editor theme |
`preset` | string | "default" | [GIF preset](#presets) |
`lineNumbers` | boolean | true | whether to show line numbers |

**Returns:** *Promise‹[Gif](#gif)›*

the Gif instance object

___

### **Gif**

### Gif.save(`filename`: string, `outDir`: string, `compression?`: undefined | "lossy" | "losless"): *Promise‹string›*

Save the GIF to the filesystem

**Parameters:**

Name | Type | Default | Description |
------ | ------ | ------ | ------ |
`filename` | string | mode + timestamp | the filename for the gif (excluding extension) |
`outDir` | string | current working directory | the output directory for the GIF |
`compression?` | undefined &#124; "lossy" &#124; "losless" | - | compression to be used on the file |

**Returns:** *Promise‹string›*

the path of the saved GIF

___

### Gif.getBuffer(): *Promise‹Buffer›*

Get the GIF's buffer

**Returns:** *Promise‹Buffer›*

the buffer for the GIF

___

### Gif.getCompressedBuffer(`lossless`: boolean): *Promise‹Buffer›*

Get the GIF's compressed buffer

**Parameters:**

Name | Type | Description |
------ | ------ | ------ |
`lossless` | boolean | whether lossless compression is required |

**Returns:** *Promise‹Buffer›*

the compressed buffer for the GIF

___

### Presets

Name | Scrolling |  Processing Time | Maximum Frames | Filesize | 
------ | ------ | ------ | ------ | ------ |
default | default (10% each frame) |  default | 100 | small | 
fast | fast (20% each frame) |  fast | 100 | very small | 
smooth | slow (2% each frame) |  slow | 100 | large | 
ultra | slow (2% each frame) |  (very) slow | 250 | (very) large | 

## Built with
- [CodeMirror](https://github.com/codemirror/CodeMirror)
- [pngjs](https://github.com/lukeapage/pngjs)
- [gif-encoder](https://github.com/twolfson/gif-encoder)
- [Puppeteer](https://github.com/puppeteer/puppeteer)

## License
[MIT](./LICENSE)

---
_Source: https://npm.io/package/code-gif-generator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
