UNPKG

@nuintun/qrcode

Version:

A pure JavaScript QRCode encode and decode library.

81 lines (76 loc) 2.23 kB
/** * @module QRCode * @package @nuintun/qrcode * @license MIT * @version 5.0.2 * @author nuintun <nuintun@qq.com> * @description A pure JavaScript QRCode encode and decode library. * @see https://github.com/nuintun/qrcode#readme */ 'use strict'; const index = require('./utils/lzw/index.cjs'); const ByteStream = require('./utils/ByteStream.cjs'); const Base64Stream = require('./utils/Base64Stream.cjs'); /** * @module GIFImage */ class GIFImage { #width; #height; #foreground; #background; #pixels = []; constructor(width, height, { foreground = [0x00, 0x00, 0x00], background = [0xff, 0xff, 0xff] } = {}) { this.#width = width; this.#height = height; this.#foreground = foreground; this.#background = background; } #encode() { const width = this.#width; const height = this.#height; const stream = new ByteStream.ByteStream(); const background = this.#background; const foreground = this.#foreground; // GIF signature: GIF89a. stream.writeBytes([0x47, 0x49, 0x46, 0x38, 0x39, 0x61]); // Logical screen descriptor. stream.writeInt16(width); stream.writeInt16(height); stream.writeBytes([0x80, 0, 0]); // Global background color palette. stream.writeBytes([background[0], background[1], background[2]]); // Global foreground color palette. stream.writeBytes([foreground[0], foreground[1], foreground[2]]); // Image descriptor. stream.writeByte(0x2c); stream.writeInt16(0); stream.writeInt16(0); stream.writeInt16(width); stream.writeInt16(height); stream.writeByte(0); // Compress pixels to stream. index.compress(this.#pixels, 2, stream); // GIF terminator. stream.writeByte(0x3b); return stream.bytes; } set(x, y, color) { this.#pixels[y * this.#width + x] = color; } toDataURL() { const bytes = this.#encode(); const stream = new Base64Stream.Base64Stream(); for (const byte of bytes) { stream.write(byte); } stream.close(); const base64 = stream.bytes; let url = 'data:image/gif;base64,'; for (const byte of base64) { url += Base64Stream.fromCharCode(byte); } return url; } } exports.GIFImage = GIFImage;