• Stars
    star
    726
  • Rank 62,418 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 10 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple, powerful and fully configurable image editor for web browsers and servers. Optional UI included.

CardKit

A simple, powerful and fully configurable image editor for web browers and servers. Optional UI included.

CardKit has three main parts:

  • CardKit: The core library, that manages and maintains the configuration object which defines the structure and options of a card
  • CardKitDOM: A DOM renderer, that takes an instance of CardKit and renders either a standalone image, or a pre-packaged UI for editing the image
  • CardKitServer: A server renderer, that allows you to take an instance of CardKit and render it into an image on a Node.js server

Additionally, a base class allows you to create your own renderers. See more in the Custom Renderers section.

Installation

$ npm install cardkit --save

Usage

CardKit requires a configuration object in order to render an image. Each renderer (CardKitDOM and CardKitServer) uses this configuration and converts it into an output. Below are simple implementations for CardKit depending on your use case.

In addition to these, you may also want to try the CardKit Yeoman Generator, which can help you scaffold an entire project in just a few moments. It brings with it the latest version of CardKit, a recommended directory structure, and a build process that helps you get your CardKit project deployed. There is also a JSFiddle that you can fork and edit for quick in-browser testing without touching the command line.

Previous versions

For version 1, see the v1-main branch.

For version 2, see the v2-main branch.

Yeoman generator

$ npm install -g yo generator-cardkit
$ yo cardkit

Browser with Webpack / Browserify usage

// Load CardKit and CardKit DOM
const CardKit = require("cardkit");
const CardKitDOM = require("cardkit/dom");

// Base configuration object - see `./examples/configurations` for examples
var configuration = {};

// Optional themes object - see `./examples/configurations` for examples
var themes = {};

// Optional layouts object - see `./examples/configurations` for examples
var layouts = {};

// Initialise CardKit
var cardkit = new CardKit(configuration, {
  themes: themes,
  layouts: layouts,
});

// Initialise Renderer
var renderer = new CardKitDOM(cardkit);

// To render the card only (with optional theme / layout overrides)
renderer.renderCard("card", {
  theme: "Alt",
  layout: "Square",
});

// OR To render the editing UI
renderer.renderUI("card");

Browser with <script> tag usage

<!-- Load in React from a CDN (or similar) -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react-dom.min.js"></script>

<!-- Load in the CardKit and CardKitDOM Libraries -->
<script
  type="text/javascript"
  src="https://cdn.rawgit.com/chrishutchinson/cardkit/v2.0.6/dist/cardkit.js"
></script>
<script
  type="text/javascript"
  src="https://cdn.rawgit.com/chrishutchinson/cardkit/v2.0.6/dist/dom.js"
></script>

<!-- Your container element to render into -->
<div id="card"></div>

<script type="text/javascript">
  // Base configuration object - see `./examples/configurations` for examples
  var configuration = {};

  // Optional themes object - see `./examples/configurations` for examples
  var themes = {};

  // Optional layouts object - see `./examples/configurations` for examples
  var layouts = {};

  // Initialise CardKit
  var cardkit = new CardKit(configuration, {
    themes: themes,
    layouts: layouts,
  });

  // Initialise Renderer
  var renderer = new CardKitDOM(cardkit);

  // To render the card only (with optional theme / layout overrides)
  renderer.renderCard("card", {
    theme: "Alt",
    layout: "Square",
  });

  // OR To render the editing UI
  renderer.renderUI("card");
</script>

Server usage

// Require CardKit and CardKitServer
const CardKit = require("cardkit");
const CardKitServer = require("cardkit/server");

// Base configuration object - see `./examples/configurations` for examples
const configuration = {};

// Initialise CardKit
const cardkit = new CardKit(configuration);

// Initialise Renderer
var renderer = new CardKitServer(cardkit);

// Render to image
renderer
  .renderToImage(2)
  .then((image) => {
    // Do what you want with the image here...
    console.log('<img src="data:image/png;base64,' + image + '" />');
    process.exit();
  })
  .catch((e) => {
    console.log("[ERR]", e);
    process.exit();
  });

APIs

CardKit

new CardKit(configuration, options)

Initialisation. Pass in a required configuration object, and optional themes, templates and layouts

cardkit.updateConfiguration(configuration, options, rerender)

Updates the configuration in your instance of CardKit. Can optionally rerender with a flag if previously rendered (supported in CardKitDOM).

cardkit.computeConfiguration(options)

Computes a configuaration object, optionally accepting a named template, theme and layout. These get merged into the base configuration and returned.

CardKitDOM

new CardKitDOM(cardkit)

Accepts an instance of CardKit and initialises the renderer

cardkit.renderUI(id, overrides)

Renders the include user interface to the specified DOM element

cardkit.renderCard(id)

Renders just the card in it's SVG form to the specified DOM element

cardkit.rerender()

Will re-render the existing UI or card

cardkit.download(scale, element)

Downloads the image to your local machine. Accepts a scale (default=2), and an element to grab from. If not provided it will fall back to the existing card being rendererd (if renderCard() was used).

CardKitServer

new CardKitDOM(cardkit)

Accepts an instance of CardKit and initialises the renderer

cardkit.renderToString()

Renders the card to a HTML string (e.g. <svg...></svg>)

cardkit.renderToImage(scale)

Renders the card to an image returning a Promise containing the image as a base64 string

Custom Renderers

A base class CardKitRenderer allows you to create your own renderer for CardKit. For example, CardKitDOM currently uses SVG to create the card, and React to render the UI. You may, however, wish to render your card using HTML canvas, or build a UI using Vue.js. Creating a custom renderer is a good way to achieve this. Below is a brief example of how you might achieve this:

class CardKitCanvas extends CardKitRenderer {
  renderCard() {
    // Canvas-specific code here
  }

  rerender() {
    // A method that `CardKit` calls if the base configuration object is updated
    // Handle an update to the base configuration, e.g. you may want to re-render the canvas element here
  }

  yourCustomMethod() {
    // You can implement any custom methods here, for example you may wish to expose or manipulate the <canvas> element for other users to take advantage of
  }
}

const cardkit = new CardKit(configuration);

const renderer = new CardKitCanvas(cardkit);

renderer.yourCustomMethod();

Custom Fonts

CardKit allows you to load in custom fonts for use on your cards, see the Wiki for details. These need to be encoded into base64 format.

If you wish to use a Google font, you can use the googlefontcss64 library to generate a base64 version of any Google font. You can use this Node.js script to get all the details you need.

Once you have the base64 encoded version of your font, you can register it in your configuration object, like so:

var configuration = {
  // ...
  fonts: {
    MyCustomFontName: base64encodedFont,
  },
  layers: {
    text: {
      fontFamily: "MyCustomFontName",
    },
  },
  // ...
};

If you need to provide a specific format for your font, you can do the following:

var configuration = {
  // ...
  fonts: {
    MyCustomFontName: {
      src: base64encodedFont,
      format: "woff",
    },
  },
  layers: {
    text: {
      fontFamily: "MyCustomFontName",
    },
  },
  // ...
};

Running locally

CardKit currently requires Node.js 14, which you can install using nvm and running:

$ nvm use

To run a sample UI locally, run: $ npm start

You can optionally pass a port like so: $ npm start -- --port=8080

Tests

To trigger the test suite, run $ npm run test

More Repositories

1

train-departure-screen

Python script to display replica real-time UK railway station departure screens for SSD13xx devices
Python
213
star
2

twentysixteen-rr

A ReactJS web app that mirrors the look, feel, and functionality of the Twenty Sixteen WordPress theme.
JavaScript
169
star
3

ios-shortcuts-demos

JavaScript
45
star
4

quickQuote-times

A tool for journalists to easily find and add video quotes to news articles. Built by @pietrop on a Times Digital Development Summer Internship.
JavaScript
35
star
5

reactpress

A React WordPress theme using the WP REST API
JavaScript
19
star
6

wp-multi-outputs

A plugin that provides a framework for multiple output formats on WordPress
PHP
15
star
7

train-delay-logger

A simple Rust command-line app for logging delayed trains (> 15 minutes) between two stations
Rust
10
star
8

sir-trevor-generator

A generator for Sir Trevor JS blocks
JavaScript
10
star
9

christmas-tree-painter

🎄 Festive Christmas tree decoration with Node.js, TypeScript and WS281x lights
TypeScript
10
star
10

slack-testing-library

A mock server and library for testing interactive Slack apps
TypeScript
7
star
11

emojipinions-wordpress

Emoji reactions / opinions for WordPress
PHP
6
star
12

talk-react-testing-library

Test like you mean it with React Testing Library
TypeScript
5
star
13

air-quality-alexa-skill

Alexa Skill for the UK Defra air quality index
JavaScript
4
star
14

soil-moisture-sensor-python

Python script for reading value of an analog soil moisture sensor via an MCP3008 and Raspberry Pi
Python
3
star
15

microsoft-todo-rss

Turn your Microsoft To Do lists into RSS feeds
TypeScript
3
star
16

acf-field-sir-trevor-js

An Advanced Custom Fields field for Sir Trevor JS
PHP
2
star
17

realthyme

First time Node.js / Mongo / Express project: to develop a real-time voting web app in a weekend (April 18th - 20th 2014).
JavaScript
2
star
18

sir-christopher-js

Custom Sir Trevor blocks
JavaScript
2
star
19

electronics-resources

Tutorials, events, software, hardware and reading material for getting started in electronics + IoT
2
star
20

react-async-status

A simple React hook for managing the status of an async action and an associated message
TypeScript
1
star
21

til

Today I learned
1
star
22

scriptornoscript

Script or No Script - A simple guessing game for JavaScript scripts, libraries and frameworks
JavaScript
1
star
23

alexa-serverless-template

A Node.js template for building Alexa skills with Serverless
JavaScript
1
star
24

site

A Next.js site for all my projects and published content
TypeScript
1
star
25

themeconf

A talk about using the WordPress REST API with Advanced Custom Fields and WordPress
JavaScript
1
star