Giter VIP home page Giter VIP logo

cardkit's Introduction

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

cardkit's People

Contributors

chrishutchinson avatar ejb avatar elliotdavies avatar gitter-badger avatar mattietk avatar peter-mouland avatar satotake avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cardkit's Issues

Different deploy architecture: library approach

Wonderful project and appreciate the easy config object(s).

The fork-as-deploy/install is limiting though. It makes it hard to config, override, and then upgrade the code base. Any changes I make to my fork might cause merge errors later.

It would be really nice if this was an installable library, where one would include the library (JS/CSS) on the page, then a bit of JS to instantiate and configure.

<html><body><script>var cardkit = new CardKit({ config: 'here' }); </script></body></html>

This would mean cardkit could be maintained in bower or npm and managed like any other dependency.

SVG filters

CardKit v1 had the ability to add / configure SVG filters. Adding this into CardKit 2 would be a valuable addition.

In CardKit v1 only one filter could be applied at a time, but it would be good if multiple filters could be layered on top of each other.

Coming soon: CardKit 2

Over the past few weeks we've been working on a major update to CardKit, version 2.

CardKit 2 will bring a range of new features, most notably:

  • Simpler to install
  • Simpler to initialise
  • Bring your own UI
  • Server-side rendering

You can find out more about all these features here: https://medium.com/digital-times/cardkit-2-d3dc1d06005#.c48hwcdcd

We'll be creating a new branch in the coming days that will allow developers a chance to try out CardKit 2.

If you have any questions, please log them as issues using the cardkit-2 label.

Add video card sizes as examples

Good suggestion from @Aendrew that CardKit could be used for creating video preview images. Add default 16:9 and 4:3 card sizes to the gh-pages build.

Also update readme and roadmap.

"download ... failed - Network error" on scaled images.

We've got 1.1.0 running, and everything had been fine. On May 17, I tweaked the themes JSON just to reorder the entries. The file still validates. The server hasn't changed.

Now we're starting to get problems, at least mostly in Chrome, though a coworker says in Firefox. My Chrome was acting up. I get a message in the download bar:
download
Failed - Network error

This is happening on my default image, which is for Facebook at 600x315 at 2x or 3x scale. It also seems to be happening on other photo sizes at 2x or 3x. My temporary fix was to build a default at 1200x630 1x, which works just fine.

I get no console errors. Chrome developer tools show three files moving: JPG, PNG, SVG + xml ... And I get the same sorta three files for actual downloads and that "Failed - Network error."

Any ideas?

Toggle to reveal elements hidden outside the bounding box of the SVG

If, for example, due to a resize using the size control, an element is now located off the canvas, turn overflow: hidden off on the SVG to reveal this and other hidden elements, allowing them to, potentially, be dragged back into view.

A checkbox in the 'Configuration' box should suffice, perhaps hidden away under an 'Advanced' toggle, as most users won't need that.

Font-awesome broken?

Just observed the Font-awesome fonts are not showing up. It used to work yesterday.

Select (logo) image source from dropdown?

Hi, really loving Cardkit and really nice to see how much easier it's gotten to configure all the things.

Just one thing I'm missing: it's be super great if I could specify a dropdown list of image sources/sizes just like I can for the fontsize of text elements.

The reason is that while a certain theme (corresponding to a brand the way we use Cardkit) will mostly be used with a certain logo, this could occasionally change. For instance, because a black and white logo of that brand would fit better with the background image.

Right now, as I understand it, I'm supposed to either duplicate the theme in themes.config,json for every combination or ask users to upload images themselves. None of those options is really ideal.

Text wrapping

As SVG <text> elements are terrible at wrapping, we may need to implement custom logic to auto-wrap text. We currently use rvg.js to do our SVG rendering, we this change may have to be part of that package, rather than part of CardKit itself.

Retina File Handling?

@chrishutchinson, thank you so much for this! My company will need to generate templated assets and this is exactly what we needed! ๐Ÿ‘

I understand that this is a complex issue and have a good idea of what all it entails, but I was curious if you had given any thought to handling images when the originating device is a retina display. Perhaps, the library could depend on Modernizr (optionally) and output images scaled according to the user's originating device. Granted, the bridge from Retina => Standard resolution is always going to throw people off. Right now, when I create an image on a Retina device and save it, opening it later in Photoshop produces a 1/2 sized image. I'm only speaking from the perspective of user's who don't understand the moving parts under the hood. For me, I plan to integrate in such a way that the images will be streamed via base64 data blob upload to my backend. It would be great to have the library configurable to send the x2 (retina) resolution image to my backend, where I can then create two assets by scaling down, rather than stretching the image and scaling the smaller one up.

Does that make sense? Have you given any thought to it? I might be able to lend a hand if we can agree on a good approach and exactly how the feature might work. Cheers!

How to offer multiple logos?

I tried CardKit v1 in the past, and am now checking out v2.

In the old system, I set up multiple theme objects in themes.config.json, each with its own logo. So, one might be "Logo normal", another might be "Logo white on transparent", etc. (I included the logos as base64 PNG data in the json)

But in v2, I don't really understand the distinctions between "layers", "layouts", and "themes".

What I'd like to do is to create several themes (to use v1 terminology), each with a different logo and color scheme. Or, failing that, maybe a drop-down select list that would let users choose between several logo options.

Rotation

It might be good to be able to rotate layers, and have that as an editable property.

Include compiled version in repo

For those that don't have node, bower, or would rather not compile their own version, we should include a pre-compiled version in the repo with easy configuration options if possible.

Add more control over canvas size

Add a sizes object to control the canvas size through drop down, e.g:

sizes = [
    {
        name: 'Facebook',
        width: 600,
        height: 380,
    },
    {
        name: 'Twitter',
        width: 620,
        height: 340,
    }
];

There's also possibly a requirement for a flexible canvas size using a range input.

Improve code coverage

We're currently sitting at 42%, with the majority of the core, server and DOM libraries covered. However most of the React UI code is not covered, and would help us improve overall coverage.

Embed code

So not quite an embed code, but the generated SVG code has all styles / fonts etc inlined, so can be directly embedded into a page.

The ability to reveal this code should be offered for those who want it.

Svg rect associated with Logo image is infinitely proliferated

Hi, Thank you for the nice tool.

[environment]

  • Mac ver: 10.10.5
  • Chrome ver: 50.0.2661.102

[specification]

  • If logo img is hovered, a svg rect is prepended.
  • If logo img is unhovered, the rect is removed.

[issue]
If logo is mousedown-ed and mouseup-ed outside logo by moving the cursor horizontally,
the svg rect is not removed and this results in inifinite proliferation of the rect

2016-06-03 11 47 04

2016-06-03 11 47 19

Add the ability to mix filters

Along with building a more visual layout for selecting filters, allow filters to be mixed / combined (e.g. blur and grayscale etc.)

Unit tests

Should definitely do this for the next release

process.EventEmitter is deprecated

Getting the following error after running grunt serve on Windows 10

(node:7524) DeprecationWarning: process.EventEmitter is deprecated. Use require('events') instead.

Have tried the fix suggested in LearnBoost/websocket.io#55, but doesn't seem to have any effect. Any ideas?

(I know this is probably low priority with v2 on the way, but any pointers would be appreciated - however obvious they may seem to you.)

Also getting stylus-type-utils was not injected in your file. error, but that looks possibly easier to fix

Loading issues on Chrome (Windows)

I have tested locally & it's working but what is the proper procedure to deploy it on a test/production server?
Also is there a way to display the previously generated images in sort of gallery?

Drag-locking on the X and / or Y axes

In CardKit v1.x the draggable property could be configured to support locking on the X or Y axes. This is currently not implemented in CardKit 2.

Potential implementation in the configuration object:

draggable: {
  x: true,
  y: false
}

Provide a way to configure elements

It would be really useful to be able to configure the elements that make up a card (currently stored in main.js). For example, the demo GIF shows a "name" element that isn't included in the app by default. That would need to be added to elements in main.js, right?

It would also be useful to be able to create predefined sets of elements for different types of cards: quote cards, photo watermarks, etc. The user could then select a "type" of card to bring up different sets of elements.

Would you accept a pull request for this sort of functionality?

Thanks very much for creating and open-sourcing CardKit! It's a fantastic tool.

Filters, all the filters

Snap.svg has some nice filters, would be good to allow those to be editable (e.g. sepia, saturation, blur etc.)

Export to SVG file

Add an additional download method for SVG so it can be opened in Illustrator.

(There will be some font issues that need to be looked at)

A smarter way of loading in web fonts

Perhaps some kind of font config file, where you can define URLs etc.

Requires fonts to be base64 encoded, so maybe that can be automatically done for those included in this config file too?

Requires more research.

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.