Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nativescript Core examples #50

Open
glorious73 opened this issue Jan 15, 2024 · 1 comment
Open

Nativescript Core examples #50

glorious73 opened this issue Jan 15, 2024 · 1 comment

Comments

@glorious73
Copy link

glorious73 commented Jan 15, 2024

I could not find a demo for Nativescript core so I created one (to save time for others). It draws both a simple black rectangle on a page and an svg from a certain source.

  • File structure:
  ...
  package.json
  app
     app-root.xml
     welcome
        welcome-page.xml
        welcome-page.js
        welcome-view-model.js (if needed)
   ...
  • package.json
{
  "name": "canvas and svg example",
  "main": "app/app.js",
  "version": "1.0.0",
  "private": true,
  "dependencies": {
    "@nativescript-community/arraybuffers": "^1.1.5",
    "@nativescript-community/ui-canvas": "^4.6.13",
    "@nativescript-community/ui-svg": "^0.1.25",
    "@nativescript/core": "~8.6.0",
    "@nativescript/theme": "^3.0.2"
  },
  "devDependencies": {
    "@nativescript/android": "8.6.2",
    "@nativescript/ios": "8.6.3",
    "@nativescript/webpack": "~5.0.18"
  }
}

ui-canvas example

  • app-root.xml
<Frame defaultPage="~/views/welcome/welcome-page" id="topmost"></Frame>
  • welcome-page.xml
<Page loaded="onLoaded" xmlns="http://www.nativescript.org/tns.xsd" xmlns:cv="@nativescript-community/ui-canvas">
    <StackLayout>
        <Label text="text before" textWrap="true" />
        <cv:CanvasView width="400" height="400" draw="onDraw"/>
        <Label text="text after" textWrap="true" />
    </StackLayout>
</Page>
  • welcome-page.js
import { Color } from "@nativescript/core";
import { Paint, createRect } from "@nativescript-community/ui-canvas";

const { WelcomeViewModel } = require("./welcome-view-model");

const welcomeViewModel = new WelcomeViewModel();

export const onLoaded = (args) => {
    const page = args.object;
    page.bindingContext = welcomeViewModel;
};

export const onDraw = (event) => {
    try {
        const paint = new Paint();
        paint.setColor(new Color('black'));
        paint.strokeWidth = 10;
        event.canvas.drawRect(createRect(0, 0, 400, 400), paint);
    }
    catch(err) {
        console.log(err);
    }
}

ui-svg example

  • Same file structure as above.

  • Note that you need to install @nativescript-community/arraybuffers, @nativescript-community/canvas, and @nativescript-community/ui-svg to avoid dependency-related errors. Also, no need for the use of Javascript since we are directly using SVGView.

  • app-root.xml

<Frame defaultPage="~/views/welcome/welcome-page" id="topmost"></Frame>
  • welcome-page.xml
<Page loaded="onLoaded" actionBarHidden="true" xmlns="http://www.nativescript.org/tns.xsd" xmlns:cv="@nativescript-community/ui-canvas" xmlns:svg="@nativescript-community/ui-svg">
    <StackLayout>
        <Label text="text before" textWrap="true" />
        <!--Make sure the src file exists-->
        <svg:SVGView height="300" src="~/assets/svg/example.svg" stretch="aspectFit" />
        <Label text="text after" textWrap="true" />
    </StackLayout>
</Page>
@farfromrefug
Copy link
Member

@glorious73 awesome will add it to the readme!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants