npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nativescript-canvas-interface

v1.0.0

Published

Nativescript Plugin to perform image manipulation using web-view canvas for Android/iOS.

Downloads

11

Readme

Nativescript-Canvas-Interface

Nativescript Plugin to perform image manipulation using web-view canvas for Android/iOS.

Installation

From the terminal, go to your app's root folder and execute:

tns plugin add nativescript-canvas-interface

Once the plugin is installed, you need to copy plugin files for webView, into your webView content folder. e.g.

cp -r node_modules/nativescript-canvas-interface/www/ app/www/lib/

Usage

For a quick start, you can check this Demo App and Blog Post

Inside Native App

Insert a web-view somewhere in your page. You can keep it hidden, if you don't want to show the image in web-view.

<Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="pageLoaded">
....
<web-view id="webView" src="~/www/index.html" visibility="collapse"></web-view>

<!-- Native Image View on which image manipulation is performed -->
<Image id="img" src="~/road-nature.jpg"/> 
....
</Page>

Initialize Canvas Interface Plugin in your javascript file.

var nsCanvasInterfaceModule = require('nativescript-canvas-interface');
var oNSCanvasInterface;
var imageView;

function pageLoaded(args){
    page = args.object;
    var webView = page.getViewById('webView');
    imageView = page.getViewById('img');
    initCanvasInterface(webView); 
}

function initCanvasInterface(webView: WebView) {
    oNSCanvasInterface = new nsCanvasInterfaceModule.NativescriptCanvasInterface(webView, 'canvasEle'); // 'canvasEle' is the value of "id" attribute of the canvas element in web-view
}

Use any API Method of NativescriptCanvasInterface Class

function setCanvasImage(){
    oNSCanvasInterface.setImage('setCanvasImage', imageView, args).then(function(result){
        // result.data contains any value returned from setCanvasImage function in web-view
    });
}

function createImage(){
    oNSCanvasInterface.createImage('setBrightness', args).then(function(result) {
        imageView.imageSource = result.image;
    });
}

If you want to set/create image on load of the page, you need to call all such code once webView is loaded

webView.on('loadFinished', (args) => {
    if (!args.error) {
        // call setImage/createImage
    }
});

Inside WebView

Import nativescript-webview-interface.js, nativescript-canvas-interface.js and es6-promise.min.js in your html page from the folder where you copied www files during installation. Add canvas element and give it an id.

<html>
    <head></head>
    <body>
        <canvas id="canvasEle"></canvas>
        <script src="path/to/es6-promise.min.js"></script>
        <script src="path/to/nativescript-webview-interface.js"></script>
        <script src="path/to/nativescript-canvas-interface.js"></script>
        <script src="path/to/your-custom-script.js"></script>        
    </body>
</html>

Now, create instance of NSCanvasInterface using canvas element. Once the instance is created, we need to register the functions which will handle requests from native app.

function init() {
    var canvasEle = document.getElementById('canvasEle');
    var oCanvasInterface = new window.NSCanvasInterface(canvasEle);
    registerNSCanvasReqHandlers(oCanvasInterface);    
}

function registerNSCanvasReqHandlers(oCanvasInterface) {
    oCanvasInterface.canvasReqHandlers = {
        setCanvasImage: setCanvasImage,
        setBrightness: setBrightness
    };
}
 
function setCanvasImage(canvas, ctx, image){
    // set image to canvas or do anything you want.
    ctx.drawImage(image, 0, 0, 100, 100);
}

/**
 * Return promise or value or nothing. 
 * Once the promise is reslved or value is returned, the plugin will create an image 
 * from canvas context and pass it to native app.
 */ 
function setBrightness(canvas, ctx, value){
    return Promise(function(resolve, reject){
        // do image manipulation on canvas
        resolve();   
    });
}   
 
init();

API

Native App API

Constructor

NativescriptCanvasInterface(webView: WebView, canvasId: String)

We need to create a new instance per web-view canvas element.

Parameters

webView: Nativescript web-view element. canvasId: Value of "id" attribute of web-view canvas element.

Methods

setImage(functionName: string, image: Image | ImageSource | string, args?: any[], format: string = 'png'): Promise<{data: any}>

Call this method to send image from native app to web-view. The image is automatically converted from nativescript ImageView/ImageSource/imagePath to HTML Image element, and that HTML Image is served to the registered function in web-view.

Parameters

functionName: Registered name of the function in web-view, to handle the image sent. image: Image to send to web-view. Image can be a Nativescript ImageView or ImageSource or a valid Image Path. args: (Optional) Any extra argument to pass to function in web-view. format: (Optional) Format in which we want to send the image to web-view. Possible formats are jpeg or png. Default value is png. returns: Promise with any data returned from the function in web-view.

createImage(functionName: string, args?: any[], format: string = 'png'): Promise<{image: ImageSource, data: any}>

Call this method to execute function in web-view, which performs canvas manipulation, and get the manipulated image back.

Parameters

functionName: Function to be executed in web-view, to create image from canvas. args: Any extra argument to pass to function in web-view. format: Expected image format from canvas in web-view. Possible formats are jpeg or png. Default value is png. returns: Promise with nativescript ImageSource and any data returned from the function in web-view.

WebView API (window.NSCanvasInterface Class)

Constructor

NSCanvasInterface(canvas: HTMLCanvasElement)

Create new instance per canvas element.

Property

canvasReqHandlers: { [fnName: string]: (...args) => Promise | any }

Register all the functions which handles requests from native app for canvas manipulation.

Signature of function which handles setImage API call from native app.
function setCanvasImage(canvas: HTMLCanvasElement, ctx: CanvasRenderingContext2D, image: HTMLImageElement, ...arg: any[]){
    // return nothing or some value or promise
}
Signature of function which handles createImage API call from native app.
function doSomeCanvasManip(canvas: HTMLCanvasElement, ctx: CanvasRenderingContext2D, ...arg: any[]){
    // return nothing or some value or promise
}