For a digital artwork I'm generating a canvas element in Vue which draws from an array of multiple images.
The images can be split in two categories:
SVG (comes with a fill-color)
PNG (just needs to be drawn as a regular image)
I came up with this:
const depict = (options) => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
const myOptions = Object.assign({}, options);
if (myOptions.ext == "svg") {
return loadImage(myOptions.uri).then((img) => {
ctx.drawImage(img, 0, 0, 100, 100);
ctx.globalCompositeOperation = "source-in";
ctx.fillStyle = myOptions.clr;
ctx.fillRect(0, 0, 100, 100);
ctx.globalCompositeOperation = "source-over";
});
} else {
return loadImage(myOptions.uri).then((img) => {
ctx.fillStyle = myOptions.clr;
ctx.drawImage(img, 0, 0, 100, 100);
});
}
};
this.inputs.forEach(depict);
for context:
myOptions.clr = the color
myOptions.uri = the url of the image
myOptions.ext = the extension of the image
While all images are drawn correctly I can't figure out why the last fillStyle overlays the whole image. I just want all the svg's to have the fillStyle which is attached to them.
I tried multiple globalCompositeOperation in different orders. I also tried drawing the svg between ctx.save and ctx.restore. No succes… I might be missing some logic here.
So! I figured it out myself in the meantime :)
I created an async loop with a promise. Inside this I created a temporary canvas per image which I then drew to one canvas. I took inspiration from this solution: https://stackoverflow.com/a/6687218/15289586
Here is the final code:
// create the parent canvas
let parentCanv = document.createElement("canvas");
const getContext = () => parentCanv.getContext("2d");
const parentCtx = getContext();
parentCanv.classList.add("grid");
// det the wrapper from the DOM
let wrapper = document.getElementById("wrapper");
// this function loops through the array
async function drawShapes(files) {
for (const file of files) {
await depict(file);
}
// if looped > append parent canvas to to wrapper
wrapper.appendChild(parentCanv);
}
// async image loading worked best
const loadImage = (url) => {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => resolve(img);
img.onerror = () => reject(new Error(`load ${url} fail`));
img.src = url;
});
};
// depict the file
const depict = (options) => {
// make a promise
return new Promise((accept, reject) => {
const myOptions = Object.assign({}, options);
var childCanv = document.createElement("canvas");
const getContext = () => childCanv.getContext("2d");
const childCtx = getContext();
if (myOptions.ext == "svg") {
loadImage(myOptions.uri).then((img) => {
childCtx.drawImage(img, 0, 0, 100, parentCanv.height);
childCtx.globalCompositeOperation = "source-in";
childCtx.fillStyle = myOptions.clr;
childCtx.fillRect(0, 0, parentCanv.width, parentCanv.height);
parentCtx.drawImage(childCanv, 0, 0);
accept();
});
} else {
loadImage(myOptions.uri).then((img) => {
// ctx.fillStyle = myOptions.clr;
childCtx.drawImage(img, 0, 0, 100, parentCanv.height);
parentCtx.drawImage(childCanv, 0, 0);
accept();
});
}
});
};
drawShapes(this.inputs);
How can I print the values of an array using three.js textGeometry. Trying the following code but no output.
`for(let i=0;i<=4;i++)
{
let arr = [1,2,3,4,5,6,7,8];
let char = arr[i];
let loader = new THREE.FontLoader();
let font = loader.parse(fontJSON);
let geometry = new THREE.TextBufferGeometry(char ,{font : font , size : 1 , height : 0.1 });
let material = new THREE.MeshBasicMaterial({ color : 0xffffff });
let text = new THREE.Mesh(geometry , material);
text.position.set(i,0,0);
scene.add(text);
}`
You have to make sure to provide a string to TextBufferGeometry, no number. You can easily ensure this by calling toString() on your char variable. I've refactored your code a bit to show you a complete example.
let camera, scene, renderer;
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 10);
camera.position.z = 8;
scene = new THREE.Scene();
const material = new THREE.MeshBasicMaterial({
color: 0xffffff
});
const arr = [1, 2, 3, 4, 5, 6, 7, 8];
const loader = new THREE.FontLoader();
loader.load('https://threejs.org/examples/fonts/helvetiker_regular.typeface.json', (font) => {
for (let i = 0; i <= 4; i++) {
const char = arr[i];
const geometry = new THREE.TextBufferGeometry(char.toString(), {
font: font,
size: 1,
height: 0.1
});
const text = new THREE.Mesh(geometry, material);
text.position.set(i, 0, 0);
scene.add(text);
}
});
renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
}
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
body {
margin: 0;
}
canvas {
display: block;
}
<script src="https://cdn.jsdelivr.net/npm/three#0.117.1/build/three.js"></script>
I have requirement which is using classbreakrenderer for showing clusters size as it breaks down, now i want to use one renderer which shows different different symbol for single cluster based on some attribute value.
I have tried to use UniqueValueRenderer but it not working.
Any suggestion
I have requirement which is using classbreakrenderer for showing clusters size as it breaks down, now i want to use one renderer which shows different different symbol for single cluster based on some attribute value.
I have tried to use UniqueValueRenderer but it not working.
I have requirement which is using classbreakrenderer for showing clusters size as it breaks down, now i want to use one renderer which shows different different symbol for single cluster based on some attribute value.
I have tried to use UniqueValueRenderer but it not working.
<script type="text/javascript">
window.dojoConfig = {
async: true,
packages: [
{
name: 'app',
location: window.location.pathname.substring(0, window.location.pathname.lastIndexOf('/')) + '/src'
}
]};
</script>
<!-- ArcGIS API for JavaScript library references -->
<script src="https://js.arcgis.com/3.15compact"></script>
<script>
require(["esri/map",
"esri/dijit/Geocoder",
"esri/dijit/HomeButton",
"esri/renderers/SimpleRenderer",
"esri/symbols/PictureMarkerSymbol",
"esri/layers/FeatureLayer",
"esri/InfoTemplate",
"esri/graphic",
"esri/graphicsUtils",
"app/clusterfeaturelayer",
"esri/symbols/SimpleMarkerSymbol",
"esri/symbols/SimpleLineSymbol",
"esri/symbols/SimpleFillSymbol",
"esri/symbols/PictureMarkerSymbol",
"esri/renderers/ClassBreaksRenderer",
"esri/renderers/HeatmapRenderer",
"esri/renderers/UniqueValueRenderer",
"dojo/_base/Color",
"dojo/on",
"dojo/dom-style",
"dojo/_base/fx",
"dojo/fx/easing",
"dojo/dom",
"dojo/domReady!"],
function (Map, Geocoder, HomeButton, SimpleRenderer, PictureMarkerSymbol, FeatureLayer, UniqueValueRenderer, InfoTemplate, Graphic, graphicsUtils, ClusterFeatureLayer, SimpleMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol, PictureMarkerSymbol, ClassBreaksRenderer, HeatmapRenderer, Color, on, domStyle, fx, easing, dom) {
// Locals
var map,
popup,
clusterLayer,
geocoder,
infoTemplate,
defaultSym,
selectedSym,
activeClusterElement;
// Create map
map = new Map("mapDiv", {
basemap: "dark-gray",
center: [-120, 50],
zoom: 3
});
// Create widget
geocoder = new Geocoder({
value: "California, United States",
autoNavigate: true,
maxLocations: 25,
autoComplete: true,
arcgisGeocoder: {
outFields: "Place_addr, PlaceName, Score"
},
map: map
}, "search");
geocoder.startup();
var home = new HomeButton({
map: map
}, "homeButton");
home.startup();
// Add raw points
// var featureLayer = new FeatureLayer("http://services.arcgis.com/oKgs2tbjK6zwTdvi/arcgis/rest/services/Major_World_Cities/FeatureServer/0", {
// infoTemplate: infoTemplate,
// outFields: ["*"]
// });
// map.addLayer(featureLayer);
// Add heatmap
// var featureLayer2 = new FeatureLayer("http://services.arcgis.com/oKgs2tbjK6zwTdvi/arcgis/rest/services/Major_World_Cities/FeatureServer/0");
// var heatmapRenderer = new HeatmapRenderer();
// featureLayer2.setRenderer(heatmapRenderer);
// map.addLayer(featureLayer2);
// Add clusters
map.on("load", function () {
// Add layer
addClusterLayer();
addClusterLayerEvents();
});
// Set popup
popup = map.infoWindow;
popup.highlight = false;
popup.titleInBody = false;
popup.domNode.className += " light";
//popup.domNode.style.marginTop = "-17px"; // for pins only
// Popup content
//infoTemplate = new InfoTemplate("Seismic Activity > 4.0", "<p>Location: ${NAME}</p><p>Magnitude: ${OTHER_MAG1}</p><p>Date: ${YEAR}/${MONTH}/${DAY}</p>");
infoTemplate = new InfoTemplate("<b>${CITY_NAME}</b>", "<p>COUNTRY: ${CNTRY_NAME}</p><p>STATE/PROVINCE: ${ADMIN_NAME}</p><p>POPULATION: ${POP}</p>");
// Option 1: Esri marker for single locations and selections
// defaultSym = new createPictureSymbol("./images/blue-cluster-pin.png", 0, 8, 9, 16);
// selectedSym = new createPictureSymbol("./images/blue-cluster-pin.png", 0, 9, 11, 20);
// Option 2: Use circle markers for symbols - Red
defaultSym = new SimpleMarkerSymbol("circle", 16,
new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([102,0,0, 0.55]), 3),
new Color([255, 255, 255, 1]));
selectedSym = new SimpleMarkerSymbol("circle", 16,
new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([102,0,0, 0.85]), 3),
new Color([255, 255, 255, 1]));
// Create a feature layer to get feature service
function addClusterLayer() {
var renderer,
renderer1,
small,
medium,
large,
xlarge;
// Add cluster renderer
clusterLayer = new ClusterFeatureLayer({
"url": "http://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/World_Cities/FeatureServer/0",
//"url": "http://services.arcgis.com/BG6nSlhZSAWtExvp/arcgis/rest/services/GlobalSeismographyNetwork/FeatureServer/0",
// "url": "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/Since_1970/MapServer/0",
"distance": 95,
"id": "clusters",
"labelColor": "#fff",
"resolution": map.extent.getWidth() / map.width,
//"singleColor": "#888",
"singleSymbol": defaultSym,
"singleTemplate": infoTemplate,
"useDefaultSymbol": false,
"zoomOnClick": false,
"showSingles": true,
"objectIdField": "FID",
outFields: ["*"]
});
renderer = new ClassBreaksRenderer(defaultSym, "clusterCount");
// Red Clusters
small = new SimpleMarkerSymbol("circle", 25,
new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([212,116,60,0.5]), 15),
new Color([212,116,60,0.75]));
medium = new SimpleMarkerSymbol("circle", 50,
new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([178,70,37,0.5]), 15),
new Color([178,70,37,0.75]));
large = new SimpleMarkerSymbol("circle", 80,
new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([144,24,13,0.5]), 15),
new Color([144,24,13,0.75]));
xlarge = new SimpleMarkerSymbol("circle", 110,
new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([102,0,0,0.5]), 15),
new Color([102,0,0,0.75]));
// Break values - can adjust easily
renderer.addBreak(2, 50, small);
renderer.addBreak(50, 250, medium);
renderer.addBreak(250, 1000, large);
renderer.addBreak(1000, 50000, xlarge);
// Providing a ClassBreakRenderer is also optional
clusterLayer.setRenderer(renderer);
//added second renderer
var renderer1 = new UniqueValueRenderer(defaultSymbol, "CNTRY_NAME");
//add symbol for each possible value
renderer1.addValue("United States", new PictureMarkerSymbol({
"angle": 0,
"xoffset": 0,
"yoffset": 8,
"type": "esriPMS",
"url": "./images/blue-pin.png",
"contentType": "image/png",
"width": 24,
"height": 24
}));
renderer1.addValue("Argentina", new PictureMarkerSymbol({
"angle": 0,
"xoffset": 0,
"yoffset": 12,
"type": "esriPMS",
"url": "./images/blue-cluster-pin.png",
"contentType": "image/png",
"width": 24,
"height": 24
}));
//added to cluster
clusterLayer.setRenderer(renderer1);
map.addLayer(clusterLayer);
}
// Create png marker
// function createPictureSymbol(url, xOffset, yOffset, xWidth, yHeight) {
// return new PictureMarkerSymbol(
// {
// "angle": 0,
// "xoffset": xOffset,
// "yoffset": yOffset,
// "type": "esriPMS",
// "url": url,
// "contentType": "image/png",
// "width": xWidth,
// "height": yHeight
// }
// );
// }
// Create new graphic and add to map.graphics
function addSelectedFeature() {
var selIndex = map.infoWindow.selectedIndex,
selFeature;
if (selIndex !== -1) {
selFeature = map.infoWindow.features[selIndex];
// Remove old feature first
removeSelectedFeature();
// Add new graphic
map.infoWindow._lastSelected = new Graphic(selFeature.toJson());
map.infoWindow._lastSelected.setSymbol(selectedSym);
map.graphics.add(map.infoWindow._lastSelected);
}
}
// Remove graphic from map.graphics
function removeSelectedFeature() {
if (map.infoWindow._lastSelected) {
map.graphics.remove(map.infoWindow._lastSelected);
map.infoWindow._lastSelected = null;
}
}
// Highlight clusters
function setActiveClusterOpacity(elem, fillOpacity, strokeOpacity) {
var textElm;
if (elem) {
elem.setAttribute("fill-opacity", fillOpacity);
elem.setAttribute("stroke-opacity", strokeOpacity);
// Overide inherited properties for the text in the circle
textElm = elem.nextElementSibling;
if (textElm && textElm.nodeName === "text") {
textElm.setAttribute("fill-opacity", 1);
}
}
}
// Hide popup if selected feature is clustered
function onClustersShown(clusters) {
var i = 0,
extent;
if (map.infoWindow.isShowing && map.infoWindow._lastSelected) {
for (i; i < clusters.length; i++) {
if (clusters[i].attributes.clusterCount > 1) {
extent = clusterLayer._getClusterExtent(clusters[i]);
if (extent.contains(map.infoWindow._lastSelected.geometry)) {
map.infoWindow.hide();
break;
}
}
}
}
}
// Wire cluster layer events
function addClusterLayerEvents() {
// Mouse over events
clusterLayer.on("mouse-over", onMouseOverCluster);
clusterLayer.on("mouse-out", onMouseOutCluster);
// Clusters drawn
clusterLayer.on("clusters-shown", onClustersShown);
}
// Save the last selected graphic so we can highlight it
map.infoWindow.on("selection-change", function () {
addSelectedFeature();
animateInfoWindow();
});
// Clear selected graphic when infoWindow is hidden
map.infoWindow.on("hide", function () {
// re-activate cluster
setActiveClusterOpacity(activeClusterElement, 0.75, 0.5);
removeSelectedFeature();
});
// Popup enhancements
function onMouseOverCluster(e) {
if (e.graphic.attributes.clusterCount === 1) {
e.graphic._graphicsLayer.onClick(e);
} else {
if (e.target.nodeName === "circle") {
activeClusterElement = e.target;
setActiveClusterOpacity(activeClusterElement, 1, 1);
} else {
setActiveClusterOpacity(activeClusterElement, 1, 1);
}
}
}
function onMouseOutCluster(e) {
if (e.graphic.attributes.clusterCount > 1) {
if (e.target.nodeName === "circle" || e.target.nodeName === "text") {
setActiveClusterOpacity(activeClusterElement, 0.75, 0.5);
setActiveClusterOpacity(e.target, 0.75, 0.5);
}
}
}
function animateInfoWindow() {
domStyle.set(map.infoWindow.domNode, "opacity", 0);
fx.fadeIn({node: map.infoWindow.domNode,
duration: 150,
easing: easing.quadIn}).play();
}
// Click to close
map.on('click', function () {
if (map.infoWindow.isShowing) {
map.infoWindow.hide();
}
});
// ESC is pressed
map.on('key-down', function (e) {
if (e.keyCode === 27) {
map.infoWindow.hide();
}
});
// Dynamically reposition popups when map moves
map.on('extent-change', function () {
if (map.infoWindow.isShowing) {
map.infoWindow.reposition();
}
});
// Auto recenter map - optional
autoRecenter(map);
function autoRecenter(map) {
on(map, 'load', function (map) {
on(window, 'resize', map, map.resize);
});
on(map, 'resize', function(extent, width, height) {
map.__resizeCenter = map.extent.getCenter();
setTimeout(function() {
map.centerAt(map.__resizeCenter);
}, 100);
});
}
});
</script>
</head>
<body>
<div id="mapDiv"></div>
<div id="search"></div>
<div id="homeButton"></div>
</body>
</html>
Used Library: dart_pdf After searching I found the same issue in GITHUB but unable to resolve the issue. I tried this but blurry image appears. Please help!!
ByteData data = await rootBundle.load('assets/test.jpg');
var codec = await instantiateImageCodec(data.buffer.asUint8List());
var frame = await codec.getNextFrame();
var imageBytes = await frame.image.toByteData();
PdfImage assetImage = PdfImage(pdf.document,
image: imageBytes.buffer.asUint8List(), width: 86, height: 80);
Rendered Image:
Use this instead:
final PdfImage assetImage = await pdfImageFromImageProvider(
pdf: pdf.document,
image: const AssetImage('assets/test.jpg'),
);
This function will create your pdf with image and custom data
var pdf = new pw.Document();
Future<pw.Document> createPDF() async {
var assetImage = pw.MemoryImage(
(await rootBundle.load('assets/images/delivery.png'))
.buffer
.asUint8List(),
);
pdf.addPage(pw.Page(
pageFormat: PdfPageFormat.a4,
build: (pw.Context context) {
var width = MediaQuery.of(this.context).size.width;
var height = MediaQuery.of(this.context).size.height;
return pw.Container(
margin: pw.EdgeInsets.only(top: height * 0.1),
child: pw.ListView(
children: [
// your image here
pw.Container(
height: height * 0.25, child: pw.Image(assetImage)),
// other contents
pw.Row(
mainAxisAlignment: pw.MainAxisAlignment.spaceAround,
children: [
pw.Text("order Id:"),
pw.Text(widget.doc['orderId']),
],
),
],
),
);
}));
return pdf;
}
use this function to save
Future savePdf(pw.Document pdfnew) async {
String pdfName;
File file;
try {
var documentDirectory = await AndroidPathProvider.downloadsPath; // for android downloads folder
// var localDirectory = await getApplicationDocumentsDirectory(); // for local directory
setState(() {
pdfName = "your_pdf_name";
});
file = File("$documentDirectory/$pdfName.pdf");
await file.writeAsBytes(await pdf.save());
return file.path;
} catch (e) {
print(e);
}
}
Can someone please help me out of this problem. What's wrong in the above code it will not shows any error but cluster layer not displaying.Remaining features are working.I am using this example https://github.com/nickcam/FlareClusterLayer
var dojoConfig = {
async: true,
packages: [{
name: 'extras',
location: location.pathname.replace(/[^\/]+$/, '') + 'JS/extras'
}]
};
var findTask, findParams;
ready(function () {
parser.parse();
var clusterLayer;
// registry.byId("ddlDistrict").on("onchange", doFind);
var intialextent = new Extent(8245227.8765913, 1297819.43274543, 8758703.79511306, 2095175.01113784, new SpatialReference({ wkid: 102100 }));
var AgricultureBoundary = new ArcGISDynamicMapServiceLayer("http://[myserver]/arcgisserver/rest/services/CRD/CRD1/MapServer", {
opacity: 0.75
});
var pointFeatureLayer = new FeatureLayer("http://[myserver]/arcgisserver/rest/services/CRD/CRD2/FeatureServer/0", {
id: "Points"
});
map = new Map("mapDiv", {
center: [77.2, 14],
zoom: 7,
extent: intialextent,
basemap: "streets",
});
map.addLayers([AgricultureBoundary, pointFeatureLayer]);
var queryTask = new esri.tasks.QueryTask("http://[myserver]/arcgisserver/rest/services/CRD/CRD2/FeatureServer/0");
var query = new esri.tasks.Query();
query.returnGeometry = true;
query.where = "pointcollected = 'No'";
query.outFields = ["*"];
dojo.connect(queryTask, "onComplete", function (featureSet) {
var inputInfo = {};
inputInfo.data = dojo.map(featureSet.features, function (feature) {
var pointX = feature.geometry.x;
var pointY = feature.geometry.y;
var att = feature.attributes;
return { "x": pointX, "y": pointY, "attributes": att };
});
clusterLayer = new ClusterFeatureLayer({
"data": inputInfo.data,
"distance": 1,
"id": "clusters",
"labelColor": "#fff",
"labelOffset": 10,
"resolution": map.extent.getWidth() / map.width,
"singleColor": "#888",
"singleTemplate": infoTemplate
});
var defaultSym = new esri.symbol.SimpleMarkerSymbol().setSize(4);
var renderer = new esri.renderer.ClassBreaksRenderer(defaultSym, "clusterCount");
var blue = new esri.symbol.PictureMarkerSymbol("http://static.arcgis.com/images/Symbols/Shapes/BluePin1LargeB.png", 32, 32).setOffset(0, 15);
var green = new esri.symbol.PictureMarkerSymbol("http://static.arcgis.com/images/Symbols/Shapes/GreenPin1LargeB.png", 64, 64).setOffset(0, 15);
var red = new esri.symbol.PictureMarkerSymbol("http://static.arcgis.com/images/Symbols/Shapes/RedPin1LargeB.png", 72, 72).setOffset(0, 15);
renderer.addBreak(0, 2, blue);
renderer.addBreak(2, 200, green);
renderer.addBreak(200, 1001, red);
clusterLayer.setRenderer(renderer);
map.addLayer(clusterLayer);
});
});
Passing data in the constructor like that won't do anything (although it probably should handle it).
Try the below instead:
dojo.connect(queryTask, "onComplete", function (featureSet) {
var inputInfo = {};
inputInfo.data = dojo.map(featureSet.features, function (feature) {
var pointX = feature.geometry.x;
var pointY = feature.geometry.y;
var att = feature.attributes;
return { "x": pointX, "y": pointY, "attributes": att };
});
clusterLayer = new ClusterFeatureLayer({
"distance": 1,
"id": "clusters",
"labelColor": "#fff",
"labelOffset": 10,
"resolution": map.extent.getWidth() / map.width,
"singleColor": "#888",
"singleTemplate": infoTemplate
});
var defaultSym = new esri.symbol.SimpleMarkerSymbol().setSize(4);
var renderer = new esri.renderer.ClassBreaksRenderer(defaultSym, "clusterCount");
var blue = new esri.symbol.PictureMarkerSymbol("http://static.arcgis.com/images/Symbols/Shapes/BluePin1LargeB.png", 32, 32).setOffset(0, 15);
var green = new esri.symbol.PictureMarkerSymbol("http://static.arcgis.com/images/Symbols/Shapes/GreenPin1LargeB.png", 64, 64).setOffset(0, 15);
var red = new esri.symbol.PictureMarkerSymbol("http://static.arcgis.com/images/Symbols/Shapes/RedPin1LargeB.png", 72, 72).setOffset(0, 15);
renderer.addBreak(0, 2, blue);
renderer.addBreak(2, 200, green);
renderer.addBreak(200, 1001, red);
clusterLayer.setRenderer(renderer);
map.addLayer(clusterLayer);
clusterLayer.addData(inputInfo.data);
});