项目作者: nativescript-community

项目描述 :
:pencil: NativeScript plugin to provide a way to capture any drawing (signatures are a common use case) from the device
高级语言: JavaScript
项目地址: git://github.com/nativescript-community/nativescript-drawingpad.git
创建时间: 2016-02-07T22:40:09Z
项目社区:https://github.com/nativescript-community/nativescript-drawingpad

开源协议:Apache License 2.0

下载



NativeScript DrawingPad



NativeScript plugin to provide a way to capture any drawing (signatures are a common use case) from the device. You can use this component to capture really anything you want that can be drawn on the screen.



Action Build


npm


npm


Installation

From your command prompt/termial go to your app’s root folder and execute:

NativeScript 7+:

  1. ns plugin add @nativescript-community/drawingpad

NativeScript < 7:

  1. tns plugin add nativescript-drawingpad

Samples

Android iOS
Sample1 Sample2

Native Libraries:

Android iOS
gcacace/android-signaturepad SignatureView

Video Tutorial

Egghead lesson - https://egghead.io/lessons/javascript-capture-drawings-and-signatures-in-a-nativescript-app

Written Tutorial

Blog post using Angular - http://tylerablake.com/nativescript/2019/05/02/capturing-signatures.html

Usage

XML:

  1. <Page xmlns="http://schemas.nativescript.org/tns.xsd" xmlns:DrawingPad="@nativescript-community/drawingpad" loaded="pageLoaded">
  2. <ActionBar title="NativeScript-DrawingPad" ></ActionBar>
  3. <ScrollView>
  4. <StackLayout>
  5. <DrawingPad:DrawingPad
  6. height="400"
  7. id="drawingPad"
  8. penColor="{{ penColor }}" penWidth="{{ penWidth }}" ></DrawingPad:DrawingPad>
  9. </StackLayout>
  10. </ScrollView>
  11. </Page>

TS:

  1. import { Frame, ImageSource } from '@nativescript/core';
  2. import { DrawingPad } from '@nativescript-community/drawingpad';
  3. // To get the drawing...
  4. public getMyDrawing() {
  5. const drawingPad = Frame.topmost().getViewById('myDrawingPad');
  6. drawingPad.getDrawing().then((res) => {
  7. console.log(res);
  8. // At this point you have a native image (Bitmap on Android or UIImage on iOS)
  9. // so lets convert to a NS Image using the ImageSource
  10. const img = new ImageSource(res); // this can be set as the `src` of an `Image` inside your NSapplication now.
  11. // now you might want a base64 version of the image
  12. const base64imageString = image.toBase64String('jpg'); // if you need it as base64
  13. });
  14. }
  15. // If you want to clear the signature/drawing...
  16. public clearMyDrawing() {
  17. const drawingPad = Frame.topmost().getViewById('myDrawingPad');
  18. drawingPad.clearDrawing();
  19. }

Angular:

  1. import { Component, ElementRef, ViewChild } from '@angular/core';
  2. import { registerElement } from '@nativescript/angular';
  3. import { ImageSource } from '@nativescript/core';
  4. import { DrawingPad } from '@nativescript-community/drawingpad';
  5. registerElement('DrawingPad', () => DrawingPad);
  6. @Component({
  7. selector: 'drawing-pad-example',
  8. template: `
  9. <ScrollView>
  10. <StackLayout>
  11. <DrawingPad
  12. #DrawingPad
  13. height="400"
  14. id="drawingPad"
  15. penColor="#ff4081"
  16. penWidth="3"
  17. >
  18. </DrawingPad>
  19. <StackLayout orientation="horizontal">
  20. <Button text="Get Drawing" (tap)="getMyDrawing()"></Button>
  21. <Button text="Clear Drawing" (tap)="clearMyDrawing()"></Button>
  22. </StackLayout>
  23. </StackLayout>
  24. </ScrollView>
  25. `
  26. })
  27. export class DrawingPadExample {
  28. @ViewChild('DrawingPad') DrawingPad: ElementRef;
  29. getMyDrawing(args) {
  30. // get reference to the drawing pad
  31. const pad = this.DrawingPad.nativeElement;
  32. // then get the drawing (Bitmap on Android) of the drawingpad
  33. let drawingImage;
  34. pad.getDrawing().then((data) => {
  35. console.log(data);
  36. // At this point you have a native image (Bitmap on Android or UIImage on iOS)
  37. // so lets convert to a NS Image using the ImageSource
  38. const image = new ImageSource(data); // this can be set as the `src` of an `Image` inside your NS
  39. drawingImage = image; // to set the src of an Image if needed.
  40. // now you might want a base64 version of the image
  41. const base64imageString = image.toBase64String('jpg'); // if you need it as base64
  42. console.log('::IMG_BASE64::', base64imageString);
  43. },
  44. (err) => {
  45. console.log(err);
  46. }
  47. );
  48. }
  49. clearMyDrawing(args) {
  50. const pad = this.DrawingPad.nativeElement;
  51. pad.clearDrawing();
  52. }
  53. }

Properties

penColor - (Color) - optional
Property to specify the pen (stroke) color to use.

penWidth - (int) - optional
Property to specify the pen (stroke) width to use.

clearOnLongPress - (boolean = true) - optional iOS Only
Gets/sets whether a long press will clear the view.

Methods

getDrawing() - Promise (returns image if successful)

getDrawingAsBase64(format?: “png” | “jpg” | “jpeg”) - Promise (returns image as base64 string if successful)

clearDrawing() - clears the drawing from the DrawingPad view.

getDrawingSvg() - Promise (returns a Scalable Vector Graphics document)

Android Only

  • getTransparentDrawing() - Promise (returns a bitmap with a transparent background)