@zappar/sharing
v1.1.7
Published
This module allows you to easily save & share snapshots or videos of a WebGL canvas.
Downloads
26
Readme
Zappar Sharing
This package allows you to easily implement a snapshot or video save/share functionality into your WebGL applications. It includes the ability to save the result and, where available, the native Web Share API makes it possible to social share to other apps installed on the device.
You may also be interested in:
- Zappar for ThreeJS (@zappar/zappar-threejs)
- Zappar for AFrame (@zappar/zappar-aframe)
- Zappar's library for Unity
- Zappar for JavaScript (@zappar/zappar), if you'd like to build content with a different 3D rendering platform
- ZapWorks Studio, a full 3D development environment built for AR, VR and MR
Table Of Contents
Starting Development
You can use this library by linking to our CDN, or by installing from NPM for use in a webpack project.
Installation
Using NPM
Run the following NPM command inside your project directory:
npm install @zappar/sharing
CDN
Reference zappar-sharing.min.js
from your HTML like this:
<script src="https://libs.zappar.com/zappar-sharing/1.1.7/zappar-sharing.min.js"></script>
Usage
Importing
Import the library into your JavaScript or TypeScript files:
import ZapparSharing from '@zappar/sharing';
Capturing the Canvas
This should be done after WebGL renders a frame or with
preserveDrawingBuffer
enabled.
Add this snippet into the event in which you would like the Save & Share Dialog to appear:
// Get canvas from dom
const canvas = document.querySelector('canvas');
// Convert canvas data to url
const url = canvas!.toDataURL('image/jpeg', 0.8);
// Take snapshot
ZapparSharing({
data: url,
});
Recording the canvas
You may use @zappar/video-recorder
for video recording.
import * as ZapparVideoRecorder from '@zappar/video-recorder';
// ...
const canvas = document.querySelector('canvas');
const recorder = await ZapparVideoRecorder.createCanvasVideoRecorder(canvas, {
quality: 25,
speed: 10,
halfSample: true,
});
recordButton.addEventListener('click', () => {
recorder.start();
});
stopRecordButton.addEventListener('click', () => {
recorder.stop();
});
recorder.onComplete.bind(async (res) => {
ZapparSharing({
data: await res.asDataURL(),
});
});
There are patent licensing implications to the use and deployment of the recorder package. Find out more about licensing and the usage over at https://www.npmjs.com/package/@zappar/video-recorder.
Getting Callbacks from the Save & Share Dialog
// ...
ZapparSharing({
data: url,
onSave: () => {
console.log('Image was saved');
},
onShare: () => {
console.log('Share button was pressed');
},
onClose: () => {
console.log('Dialog was closed');
},
});
The arguments indicate if the user tapped on the Save or Share buttons in that dialog, but do not guarantee that the user actually completed those actions.
onClose
is called when the user returns from the Save/Share dialog.
Customization
// ...
ZapparSharing({
data: url,
fileNamePrepend: 'Zappar', // The name of the file.
shareTitle: 'Hello World!', // The title for the social share.
shareText: 'Hello World!', // The body text for the social share.
shareUrl: 'www.zappar.com', // The url for the social share.
hideShareButton: true, // Hide the share button.
});
Editing Pre-Defined Styles
The package comes with a pre-defined style which you can override.
This can be done by passing a style object as a parameter as seen in the example.
ZapparSharing({
data: url,
hideShareButton: true,
}, {
buttonCloseAnchor: {
width: '15px',
height: '15px',
},
});
saveShareAnchor: {
display: 'flex',
width: '70px',
height: '70px',
marginTop: '2.5%',
marginLeft: '5%',
marginRight: '5%',
},
buttonImage: {
pointerEvents: 'none',
display: 'flex',
justifyContent: 'center',
margin: 'auto',
width: '40px',
height: '40px',
},
buttonCloseAnchor: {
width: '15px',
height: '15px',
margin: '4%',
zIndex: 9999,
top: 0,
position: 'absolute',
},
previewElement: {
height: 'auto',
width: '80%',
marginLeft: 'auto',
marginRight: 'auto',
backgroundColor: '#ccc',
boxShadow: '0px 0px 4px 0px rgba(0,0,0,0.5)',
display: 'flex',
},
containerDiv: {
position: 'fixed',
width: '100%',
height: '100%',
top: '0px',
left: '0px',
zIndex: 10000,
backgroundColor: 'rgba(255,255,255,1)',
fontFamily: 'sans-serif',
color: 'rgba(255,255,255,1)',
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',
},
flexContainerDiv: {
display: 'flex',
justifyContent: 'center',
flexWrap: 'wrap',
},
buttonCloseImage: {
pointerEvents: 'none',
width: '15px',
height: '15px',
},
buttonOpenFiles: {
border: '2px solid black',
textTransform: 'uppercase',
padding: '10px',
minWidth: '50px',
color: 'black',
display: 'inline - block',
marginTop: '20px',
textDecoration: 'none',
borderRadius: '10px',
paddingTop: '15px',
},
instructions: {
color: 'black',
marginBottom: '0',
width: '100%',
textAlign: 'center',
display: 'none',
},
Localizations
Custom text or localizations can be implemented:
// ...
ZapparSharing({
data: url,
hideShareButton: true,
}, {}, {
SAVE: 'SAVE',
SHARE: 'SHARE',
NowOpenFilesAppToShare: 'Now open files app to share',
TapAndHoldToSave: 'Tap and hold the image<br/>to save to your Photos app',
});
Example
Using the snippets above, your script should now include:
// Get canvas from dom
const canvas = document.querySelector('canvas');
// Convert canvas data to url
const url = canvas!.toDataURL('image/jpeg', 0.8);
ZapparSharing({
data: url,
fileNamePrepend: 'Zappar',
shareUrl: 'www.zappar.com',
shareTitle: 'Hello World!',
shareText: 'Hello World!',
onSave: () => {
console.log('Image was saved');
},
onShare: () => {
console.log('Share button was pressed');
},
onClose: () => {
console.log('Dialog was closed');
},
}, {}, {
SAVE: 'SAVE',
SHARE: 'SHARE',
NowOpenFilesAppToShare: 'Now open files app to share',
TapAndHoldToSave: 'Tap and hold the image<br/>to save to your Photos app',
});