diff --git a/hello-world/svelte/README.md b/hello-world/svelte/README.md index e6cd94fc..ce80768d 100644 --- a/hello-world/svelte/README.md +++ b/hello-world/svelte/README.md @@ -1,47 +1,355 @@ -# Svelte + TS + Vite +# Hello World for Vite + Svelte + TS - Dynamsoft Barcode Reader Sample -This template should help get you started developing with Svelte and TypeScript in Vite. +[Svelte](https://svelte.dev/) is a JavaScript library meant explicitly for creating interactive UIs. Svelte compiles components into code that directly manipulates the DOM, unlike other frameworks such as Vue and React that relies on a virtual DOM for updates. Follow this guide to learn how to implement [Dynamsoft Barcode Reader JavaScript SDK](https://www.dynamsoft.com/barcode-reader/sdk-javascript/) (hereafter called "the library") into a Svelte application using Vite. Note that in this sample we will use TypeScript. -## Recommended IDE Setup +In this guide, we will be using [`dynamsoft-barcode-reader-bundle 10.2.1000`](https://www.npmjs.com/package/dynamsoft-barcode-reader-bundle/v/10.2.1000). -[VS Code](https://code.visualstudio.com/) + [Svelte](https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode). +> Note: +> +> If you’re looking to integrate DBR-JS into a framework that we don't yet have a sample, don't worry! We have a [comprehensive guide](https://www.dynamsoft.com/barcode-reader/docs/web/programming/javascript/user-guide/use-in-framework.html) that provides detailed instruction and best practices for a seamless integration into any frameworks! +> +> Additionally, we're here to help! Please don't hesitate to [contact us](#Support) for any support or questions you might have. -## Need an official Svelte framework? +## Official Sample -Check out [SvelteKit](https://github.com/sveltejs/kit#readme), which is also powered by Vite. Deploy anywhere with its serverless-first approach and adapt to various platforms, with out of the box support for TypeScript, SCSS, and Less, and easily-added support for mdsvex, GraphQL, PostCSS, Tailwind CSS, and more. +* Hello World for Vite + Svelte + TS - Source Code -## Technical considerations +## Preparation -**Why use this over SvelteKit?** +Make sure you have [node](https://nodejs.org/) installed. `node 16.20.1` and `svelte 4.2.12` are used in the example below. -- It brings its own routing solution which might not be preferable for some users. -- It is first and foremost a framework that just happens to use Vite under the hood, not a Vite app. +## Quick Start -This template contains as little as possible to get started with Vite + TypeScript + Svelte, while taking into account the developer experience with regards to HMR and intellisense. It demonstrates capabilities on par with the other `create-vite` templates and is a good starting point for beginners dipping their toes into a Vite + Svelte project. +```cmd +npm install +npm run +``` +Then open http://localhost:5173/ to view the sample app. + +## Create the sample project + +If you're planning to start the project from scratch, in this section, we will be creating a Svelte application with Vite utilizing the Dynamsoft Barcode Reader bundle sdk. + +We'll be exploring how you could create a page that not only enables barcode scanning via a webcam or a built-in camera, but also decode barcodes from local images. -Should you later need the extended capabilities and extensibility provided by SvelteKit, the template has been structured similarly to SvelteKit so that it is easy to migrate. +By the end of this guide, you'll have a good understanding of the SDK and be ready to discover more ways to use it! -**Why `global.d.ts` instead of `compilerOptions.types` inside `jsconfig.json` or `tsconfig.json`?** +### Create a Bootstrapped Svelte Application with Vite and TypeScript -Setting `compilerOptions.types` shuts out all other types not explicitly listed in the configuration. Using triple-slash references keeps the default TypeScript setting of accepting type information from the entire workspace, while also adding `svelte` and `vite/client` type information. +```cmd +npm create vite@latest my-app -**Why include `.vscode/extensions.json`?** + +√ Select a framework: » Svelte +√ Select a variant: » TypeScript +``` + +### **CD** to the root directory of the application and install necessary libraries + +```cmd +cd my-app +npm install dynamsoft-barcode-reader-bundle -E +``` -Other templates indirectly recommend extensions via the README, but this file allows VS Code to prompt the user to install the recommended extension upon opening the project. +## Start to implement -**Why enable `allowJs` in the TS template?** +### Add file "dynamsoft.config.ts" under "/src/" to configure libraries -While `allowJs: false` would indeed prevent the use of `.js` files in the project, it does not prevent the use of JavaScript syntax in `.svelte` files. In addition, it would force `checkJs: false`, bringing the worst of both worlds: not being able to guarantee the entire codebase is TypeScript, and also having worse typechecking for the existing JavaScript. In addition, there are valid use cases in which a mixed codebase may be relevant. +```typescript +/* /src/dynamsoft.config.ts */ +import { CoreModule } from "dynamsoft-core"; +import { LicenseManager } from "dynamsoft-license"; +import "dynamsoft-barcode-reader"; -**Why is HMR not preserving my local component state?** +// Configures the paths where the .wasm files and other necessary resources for modules are located. +CoreModule.engineResourcePaths = { + std: "https://cdn.jsdelivr.net/npm/dynamsoft-capture-vision-std@1.2.10/dist/", + dip: "https://cdn.jsdelivr.net/npm/dynamsoft-image-processing@2.2.30/dist/", + core: "https://cdn.jsdelivr.net/npm/dynamsoft-core@3.2.30/dist/", + license: "https://cdn.jsdelivr.net/npm/dynamsoft-license@3.2.21/dist/", + cvr: "https://cdn.jsdelivr.net/npm/dynamsoft-capture-vision-router@2.2.30/dist/", + dbr: "https://cdn.jsdelivr.net/npm/dynamsoft-barcode-reader@10.2.10/dist/", + dce: "https://cdn.jsdelivr.net/npm/dynamsoft-camera-enhancer@4.0.3/dist/", +}; -HMR state preservation comes with a number of gotchas! It has been disabled by default in both `svelte-hmr` and `@sveltejs/vite-plugin-svelte` due to its often surprising behavior. You can read the details [here](https://github.com/rixo/svelte-hmr#svelte-hmr). +/** LICENSE ALERT - README + * To use the library, you need to first specify a license key using the API "initLicense()" as shown below. + */ -If you have state that's important to retain within a component, consider creating an external store which would not be replaced by HMR. +LicenseManager.initLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", true); -```ts -// store.ts -// An extremely simple external store -import { writable } from 'svelte/store' -export default writable(0) +/** + * You can visit https://www.dynamsoft.com/customer/license/trialLicense?utm_source=github&product=dbr&package=js to get your own trial license good for 30 days. + * Note that if you downloaded this sample from Dynamsoft while logged in, the above license key may already be your own 30-day trial license. + * For more information, see https://www.dynamsoft.com/barcode-reader/programming/javascript/user-guide/?ver=10.2.10&utm_source=github#specify-the-license or contact support@dynamsoft.com. + * LICENSE ALERT - THE END + */ + +// Optional. Preload "BarcodeReader" module for reading barcodes. It will save time on the initial decoding by skipping the module loading. +CoreModule.loadWasm(["DBR"]); ``` + +> Note: +> +> * `initLicense()` specify a license key to use the library. You can visit https://www.dynamsoft.com/customer/license/trialLicense?utm_source=sample&product=dbr&package=js to get your own trial license good for 30 days. +> * `engineResourcePaths` tells the library where to get the necessary resources at runtime. + +### Build directory structure + +* Create a directory `components` under `/src/` + +### Create and edit the `VideoCapture` component + +* Create `VideoCapture.svelte` under `/src/components/`. The `VideoCapture` component helps decode barcodes via camera. + +* In `VideoCapture.svelte`, add code for initializing and destroying some instances. For our stylesheet (CSS) specification, please refer to our [source code](#Official-Sample). + +```svelte + + + +
+
+
+ Results: +
+
+``` + +> Note: +> +> * If you're looking to customize the UI, the UI customization feature are provided by the auxiliary SDK "Dynamsoft Camera Enhancer". For more details, refer to our [User Guide](https://www.dynamsoft.com/barcode-reader/docs/web/programming/javascript/user-guide/index.html#customize-the-ui) + +### Create and edit the `ImageCapture` component + +* Create `ImageCapture.svelte` under `/src/components/`. The `ImageCapture` component helps decode barcodes in an image. + +* In `ImageCapture.svelte`, add code for initializing and destroying some instances. For our stylesheet (CSS) specification, please refer to our [source code](#Official-Sample). + +```svelte + + + +
+
+ +
+
+
+``` + +### Add the `VideoCapture` and `ImageCapture` component to `App.svelte` + +* On `/src/App.svelte`, we will edit the component so that it offers buttons to switch components between `VideoCapture` and `ImageCapture`. + +* Add following code to `App.svelte`. For our stylesheet (CSS) specification, please refer to our [source code](#Official-Sample). + +```svelte + + + +
+
+
+

Hello World for Svelte

+ +
+
+ + +
+ {#if mode === "video"} + + {:else} + + {/if} +
+
+``` + +* Try running the project. + +```cmd +npm run dev +``` + +If you followed all the steps correctly, you will have a working page that turns one of the cameras hooked to or built in your computer or mobile device into a barcode scanner. Also, if you want to decode a local image, just click the `Decode Image` button and select the image you want to decode. Once barcodes are found, the results will show in a dialog. + +## Support + +If you have any questions, feel free to [contact Dynamsoft support](https://www.dynamsoft.com/company/contact?utm_source=sampleReadme). \ No newline at end of file diff --git a/hello-world/svelte/index.html b/hello-world/svelte/index.html index 9073fad5..6471fdec 100644 --- a/hello-world/svelte/index.html +++ b/hello-world/svelte/index.html @@ -1,9 +1,15 @@ - + - Vite + Svelte + TS + + + Hello World for Vite + Svelte + TS - Dynamsoft Barcode Reader Sample
diff --git a/hello-world/svelte/src/App.svelte b/hello-world/svelte/src/App.svelte index 679012ba..78d16127 100644 --- a/hello-world/svelte/src/App.svelte +++ b/hello-world/svelte/src/App.svelte @@ -9,24 +9,20 @@

Hello World for Svelte

- +
-
+
Decode Video Decode Image
{#if mode === "video"} @@ -51,40 +47,41 @@ align-items: center; margin-top: 20px; } + .title .title-logo { width: 70px; height: 30px; margin-left: 10px; } - .top-btns { + .buttons-container { width: 30%; margin: 20px auto; } - .top-btns button { + .buttons-container button { display: inline-block; border: 1px solid black; padding: 5px 15px; background-color: transparent; cursor: pointer; - /* TODO */ - margin-right: -5px; + margin-right: -5px; } - .top-btns button:first-child { + .buttons-container button:first-child { border-top-left-radius: 10px; border-bottom-left-radius: 10px; border-right: transparent; } - .top-btns button:nth-child(2) { + + .buttons-container button:nth-child(2) { border-top-right-radius: 10px; border-bottom-right-radius: 10px; border-left: transparent; } - @media screen and (max-width: 500px) { - .top-btns { + @media screen and (max-width: 800px) { + .buttons-container { width: 70%; } } diff --git a/hello-world/svelte/src/components/ImageCapture.svelte b/hello-world/svelte/src/components/ImageCapture.svelte index 75735b91..1fbaf6c1 100644 --- a/hello-world/svelte/src/components/ImageCapture.svelte +++ b/hello-world/svelte/src/components/ImageCapture.svelte @@ -1,69 +1,73 @@ -
-
- +
+
+
-
+
diff --git a/hello-world/svelte/src/components/VideoCapture.svelte b/hello-world/svelte/src/components/VideoCapture.svelte index 3e49ea5e..d7393a7a 100644 --- a/hello-world/svelte/src/components/VideoCapture.svelte +++ b/hello-world/svelte/src/components/VideoCapture.svelte @@ -1,111 +1,122 @@
-
+
Results:
-
+
diff --git a/hello-world/svelte/src/dynamsoft.config.ts b/hello-world/svelte/src/dynamsoft.config.ts index ac0b2d81..24909a1c 100644 --- a/hello-world/svelte/src/dynamsoft.config.ts +++ b/hello-world/svelte/src/dynamsoft.config.ts @@ -1,12 +1,24 @@ -import { CoreModule } from 'dynamsoft-core'; -import { LicenseManager } from 'dynamsoft-license'; -import 'dynamsoft-barcode-reader'; +/* /src/dynamsoft.config.ts */ +import { CoreModule } from "dynamsoft-core"; +import { LicenseManager } from "dynamsoft-license"; +import "dynamsoft-barcode-reader"; + +// Configures the paths where the .wasm files and other necessary resources for modules are located. +CoreModule.engineResourcePaths = { + std: "https://cdn.jsdelivr.net/npm/dynamsoft-capture-vision-std@1.2.10/dist/", + dip: "https://cdn.jsdelivr.net/npm/dynamsoft-image-processing@2.2.30/dist/", + core: "https://cdn.jsdelivr.net/npm/dynamsoft-core@3.2.30/dist/", + license: "https://cdn.jsdelivr.net/npm/dynamsoft-license@3.2.21/dist/", + cvr: "https://cdn.jsdelivr.net/npm/dynamsoft-capture-vision-router@2.2.30/dist/", + dbr: "https://cdn.jsdelivr.net/npm/dynamsoft-barcode-reader@10.2.10/dist/", + dce: "https://cdn.jsdelivr.net/npm/dynamsoft-camera-enhancer@4.0.3/dist/", +}; /** LICENSE ALERT - README * To use the library, you need to first specify a license key using the API "initLicense()" as shown below. */ -LicenseManager.initLicense('DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9'); +LicenseManager.initLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", true); /** * You can visit https://www.dynamsoft.com/customer/license/trialLicense?utm_source=github&product=dbr&package=js to get your own trial license good for 30 days. @@ -15,15 +27,5 @@ LicenseManager.initLicense('DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9'); * LICENSE ALERT - THE END */ -CoreModule.engineResourcePaths = { - std: "https://cdn.jsdelivr.net/npm/dynamsoft-capture-vision-std@1.2.10/dist/", - dip: "https://cdn.jsdelivr.net/npm/dynamsoft-image-processing@2.2.30/dist/", - core: "https://cdn.jsdelivr.net/npm/dynamsoft-core@3.2.30/dist/", - license: "https://cdn.jsdelivr.net/npm/dynamsoft-license@3.2.21/dist/", - cvr: "https://cdn.jsdelivr.net/npm/dynamsoft-capture-vision-router@2.2.30/dist/", - dbr: "https://cdn.jsdelivr.net/npm/dynamsoft-barcode-reader@10.2.10/dist/", - dce: "https://cdn.jsdelivr.net/npm/dynamsoft-camera-enhancer@4.0.3/dist/" -}; - // Optional. Preload "BarcodeReader" module for reading barcodes. It will save time on the initial decoding by skipping the module loading. -CoreModule.loadWasm(['DBR']); +CoreModule.loadWasm(["DBR"]); diff --git a/hello-world/svelte/src/main.ts b/hello-world/svelte/src/main.ts index d5f003c5..8c4eaed9 100644 --- a/hello-world/svelte/src/main.ts +++ b/hello-world/svelte/src/main.ts @@ -1,7 +1,7 @@ -import App from './App.svelte' +import App from "./App.svelte"; const app = new App({ - target: document.getElementById('app')!, -}) + target: document.getElementById("app")!, +}); -export default app +export default app;