Coder Social home page Coder Social logo

nine-patch's Introduction

nine-patch

A simple nine-patch image reader for javascript.

Usage

Before using the nine-patch, you need to import the NinePatch class from the package. Then you must create a new instance of the NinePatch class and call the init method to load the image. After the image is loaded, you can call the draw method to draw the image on the element. Init method will return a promise, you can use the then method to do something after the image is loaded.

import NinePatch from '@xumumi/nine-patch'

const ninePatch = new NinePatch(url);
ninePatch.init().then(() => this.ninePatch.draw(element))

element: The element to draw the image on. url: The url of the nine-patch image.

if you want to get the image base64 data, you can use the following code:

import NinePatch from '@xumumi/nine-patch'

const ninePatch = new NinePatch(url);
ninePatch.init().then(() => {
  const base64 = ninePatch.getBase64(width, height);
  // do something with the base64 data
})

width: The width of the image. height: The height of the image.

You do not need to repeatedly call the init method to draw the image on multiple elements.

import NinePatch  from '@xumumi/nine-patch'
const ninePatch = new NinePatch(url);
await ninePatch.init();
ninePatch.draw(element1);
ninePatch.draw(element2);

If element size is changed, you can call the draw method to redraw the image.

function onResize() {
  ninePatch.draw(element);
}

Install

npm i @xumumi/nine-patch

CDN

<script src="https://unpkg.com/@xumumi/nine-patch/lib/nine-patch.min.js"></script>

Example

Clone the repository and run the following commands:

npm install
npm run dev

Then open the browser and visit the url which is displayed in the console.

Or you can visit the demo.

Reference

License

MIT

nine-patch's People

Contributors

xumumi avatar

Stargazers

 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.