Images in Scripts
Draw, load and render images in pre-export code and HTML templates, and add them to the output.
Scripts that run while you export can make images of their own: a banner with the tour’s title, thumbnails of every node, a little planet for a social media post. This works in two places:
- Pre-export code in the skin, which can put an image into an image element or write it to the output’s assets folder.
- HTML templates, which add an image to the output
with
document.addImage().
Here is a share image made from the tour’s preview with the title written on top, as an HTML template would do it:
var share = Image.preview(1200, 630, { crop: true });
var painter = share.painter();
painter.fillRect(0, 470, 1200, 160, "#b0000000");
painter.setPen("white");
painter.setFont("Helvetica", 64, true);
painter.drawTextInRect(0, 470, 1200, 160, "My Tour", "center");
document.addImage(share, "images/share.jpg", { quality: 85 });
Creating Images
new Image(width, height) creates a transparent image. Each side can be 1 to
16384 pixels.
Image.load(path) reads an image file. The path says where it comes from:
assets/...– A file in the assets folder of the output, which you set up in the Web Output Properties. In pre-export code, your assets are already copied there.skin:<name>– Only in pre-export code: a file from the Local Files of a code element, by the file name shown in the table, for exampleskin:images/star.png.template:...– Only in HTML templates: a file in the template’s own folder.project:...– A file in your project folder or below it. Save the project first.
Paths can’t leave their folder: .., absolute paths and links are not followed.
A photo’s orientation is applied, so a picture taken upright stays upright.
Image.preview(width, height, options) renders the tour’s preview image, the
same one the HTML template’s addPreviewImage() uses. With { crop: true } it
is scaled to fill exactly width x height and cut to that size.
Image.nodePreview(nodeId, width, height, options) renders a node’s preview
from its default view, filled and cut to exactly width x height. Use
{ crop: false } to keep the preview’s own shape.
Image.nodeView(nodeId, width, height, options) renders any view of a panorama
node. The options are pan, tilt, roll and fov in degrees and
projection: rectilinear (the normal view), stereographic (little planet),
fisheye, equirectangular, cylinder, mercator, angular, mirrorball or
sphere. What you leave out comes from the node’s default view. For
equirectangular, cylinder and mercator, leaving them out gives you the
whole panorama instead. Some projections have a fixed shape: an equirectangular
image is always twice as wide as it is high.
var left = Image.nodeView("node1", 800, 450, { pan: 90, tilt: -15, fov: 80 });
var planet = Image.nodeView("node1", 600, 600, {
tilt: -90,
fov: 270,
projection: "stereographic",
});
The ids of all nodes are in the nodes list, so you can make an image of each
one:
for (var i = 0; i < nodes.length; i++) {
var thumb = Image.nodePreview(nodes[i].id, 160, 90);
assets.writeImage(thumb, "assets/thumbs/" + nodes[i].id + ".jpg");
}
A 3D model node has no panorama to render, so Image.nodeView() doesn’t work
for it. Image.nodePreview() gives you its preview image.
Image Information
Image.info(path) tells you about a file without loading its pixels. The path
works as for Image.load(). You get:
name,size(in bytes) andmodified(the date of the file).width,heightandformatfor an image, with the photo’s orientation applied.- What the photo’s EXIF and XMP data say:
title,description,author,copyright,creatorTool,tags,created,gps(withlatitude,longitude, andaltitude,headinganddatewhere present),levellingandequirectangular. Only what the file has is there.
The files in assets.folders have only their name, path, size and
fromSource, so listing even a large gallery is quick. fromSource is true
for the files in your assets. It is false for what else is in the output’s
assets folder: files your code wrote in an earlier export, and copies of assets
you deleted since. These are exported, packaged and uploaded only if your code
writes them again or calls assets.keepFile(path).
Ask Image.info() for the details of the files you need:
var gallery = assets.folders.filter(function (folder) {
return folder.path === "assets/gallery";
})[0];
var photos = gallery.files
.filter(function (file) {
return file.fromSource;
})
.map(function (file) {
var info = Image.info(file.path);
info.path = file.path;
return info;
})
.filter(function (info) {
return info.width > 0;
});
assets.writeTextFile("assets/gallery/index.json", JSON.stringify(photos));
Working with Images
width(),height()– The size in pixels.isNull()– True for an empty image.fill(color)– Fills the whole image with a color.pixel(x, y)– The color of a pixel as#aarrggbb.setPixel(x, y, color)– Changes a single pixel.scaled(width, height)– A scaled copy. Leave out one side, or give0, to keep the shape.copy(x, y, width, height)– A copy of part of the image.painter()– A painter for drawing on the image, see below.
Colors can be CSS colors like "red", "#336699" or "#80ffffff" (with
alpha), numbers like 0xff336699, or Color objects.
Drawing
Get a painter with image.painter() and draw with it. When you’re done, call
painter.end(). If you forget, nothing is lost: as soon as you use the image,
the painter stops and everything you drew is in it.
setPen(color, width)– The color and width of lines and text.nulldraws no outline.setBrush(color)– The color shapes are filled with.nullfills nothing.setLinearGradient(x1, y1, x2, y2, stops)– Fills shapes with a gradient.stopsis a list of positions from 0 to 1 and colors:[[0, "black"], [1, "white"]].setFont(family, pixelSize, bold, italic)– The font for text.setOpacity(opacity)– From 0 (invisible) to 1.setAntialiasing(on)– Smooth edges, on by default.fillRect(x, y, width, height, color),clearRect(x, y, width, height)– Fills an area with a color, or makes it transparent.drawRect(),drawRoundedRect(x, y, width, height, rx, ry),drawEllipse(),drawLine(x1, y1, x2, y2)– Shapes with the current pen and brush.drawPolygon(points),drawPolyline(points)– Points as[x, y]or{x, y}.drawImage(image, x, y, width, height)– Draws another image. Leave out the size to draw it as it is.drawText(x, y, text)– Text on a baseline at y.drawTextInRect(x, y, width, height, text, align, wrap)– Text inside a box.alignis a combination ofleft,right,hcenter,top,bottom,vcenteror justcenter. Long text wraps unless wrap is false.textWidth(text),textHeight()– The size of text in the current font.translate(dx, dy),rotate(degrees),scale(sx, sy),resetTransform(),save(),restore()– Move, turn and scale what you draw next.
Adding Images to the Output
In pre-export code, assets.writeImage(image, path, options) writes an image to
the assets folder of the output. The path starts with assets/, and missing
folders are created. Images you write are packaged and uploaded with the tour;
when you skip one because it is still up to date, call assets.keepFile(path)
so it stays part of the tour. In an HTML template,
document.addImage(image, path, options) adds an image to the output like
addfile() does.
The file name’s extension chooses the format: png, jpg or webp. The
options are:
format– The format, when the file name has no extension.quality– 0 to 100 for JPEG and WebP, 85 if you leave it out.background– JPEG images have no transparency. Transparent areas get this color, white if you leave it out.
In pre-export code, you can also replace the image of an image element or a
button: elements["banner"].setImage(image). For a button, add the state: 1
for the hover image, 2 for the active one. elements["banner"].getImage()
gives you the current image to draw on. The export saves the new image in the
element’s own format and quality.
Faster Exports
Your script runs on every export, but Pano2VR remembers what it worked out before:
- The information
Image.info()read stays known while the file keeps its size and date. Image.preview(),Image.nodePreview()andImage.nodeView()render a panorama only when something it depends on changed: its image files, the node’s settings, its levelling or its patches. Otherwise you get the image from the last export right away.- A file you write with the same content as before is left as it is, with its old date, so uploading the tour again only sends what really changed.
With
Unload images to save memory
on, a panorama that a script had to load is unloaded again right after its image
is made. For an image of every node in a large tour, use
exporter.eachNode()
in pre-export code: it runs while the export has each panorama loaded anyway.
Errors
A mistake in a script, like an unknown node, a file that isn’t there, or a
misspelled option, stops the script with an error that names the line. You can
catch it with try { ... } catch (e) { ... }. Errors that aren’t caught, and
images that can’t be written, are listed with the export’s other problems when
the export is done.