How to cache an image and use it later in EaselJS? - css-sprites

I have a very basic sprite animation. How to cache an image and use it in the "images" part of the sprite sheet creation?
I tried with:
Image1= new Image(2779,1135)
Image1.src = "img/contur.png"
but I don't knwo how to include Image 1 in the "images" section.
stage = new createjs.Stage(document.getElementById("testCanvas"));
stage.clear();
var ss = new createjs.SpriteSheet({
"animations": {"run": [0, 50]},
"images": ["img/contur.png"],
"frames":
{
"height": 140,
"width": 231,
"regX": 0,
"regY": 0,
"count": 51
}
});
var grant = new createjs.Sprite(ss, "run");
stage.addChild(grant);
createjs.Ticker.setFPS(24);
createjs.Ticker.addEventListener("tick", stage);

If I understood correctly your question, you want to load the image prior to its usage in the spritesheet.
You have 2 ways to do this:
Add a callback on img.load:
img = new Image();
img.src = "img/contur.png";
img.onload = function(event) {
// you can create spritesheet here
}
Using PreloadJS (recommended):
loader = new createjs.LoadQueue(true);
loader.on('complete', function(event) {
// you can create spritesheet here
});
loader.loadManifest([{id:'imageX', src:'img/contur.png'}]);
if that is the case, you also need to run a web server (for example, using python: python -m SimpleHTTPServer 8088)
For both options, you just need to use the path to image (the browser will get the preloaded image automagically):
var ss = new createjs.SpriteSheet({
"animations": {"run": [0, 50]},
"images": ["img/contur.png"],
"frames": {
"height": 140,
"width": 231,
"regX": 0,
"regY": 0,
"count": 51
}
});
If you are using PreloadJS, you can specify the loaded image object by an alias (defined on manifest):
"images": [loader.getResult('imageX')],
...
NOTE: remember to call stage.update() after adding stuff to the stage or to animate the frames, otherwise it will simply show an empty canvas or a static image.

Related

Multiple html2canvas div content in single .pdf file

I'm using attached script to allocate content of div into .pdf file with jsPDF and html2canvas.
All works nicely with content of one single div (inputdiv).
However, I would like to add conent from few more additional div's into same .pdf file.
Question is how to do that?
With approach from below, only content of single div can be mapped through relevant canvas function. I tried to find some extra details in documentation and also here but unfortunately without success.
Any reference would be highly appreciated!
function generatePDF() {
var doc = new jsPDF ("l", "mm", "a4", "true");
html2canvas(inputdiv, {
width: 2000,
height: 1450,
scale: 2,
onclone: function (clonedDoc) {
clonedDoc.getElementById('inputdiv').style.display = 'block';
}
})
.then(function(canvas) {
var imgData = canvas.toDataURL('image/jpeg',1.0);
const imgProps= doc.getImageProperties(imgData);
const pdfWidth = doc.internal.pageSize.getWidth();
const pdfHeight = (imgProps.height * pdfWidth) / imgProps.width;
doc.addImage(imgData, 'jpeg', 14, 19, pdfWidth, pdfHeight, undefined, 'FAST');
doc.save('File.pdf');
});
}
This is an example of how you can solve this, for 2 divs that will print on 2 pages.
Of course if you have many divs, it would need to be refactored to avoid code repetition; but this one worked for me to include 2 different divs.
html2canvas(div1)
.then((canvasDiv1) =>{
//...
pdf.addImage(/* ... */);
})
.then(() => {
pdf.addPage();
html2canvas(div2)
.then((canvasDiv2) => {
//...
pdf.addImage(/* ... */);
// save on the last element
pdf.save(/* ... */);
})
})
If you need both divs to be on the same page, then just remove the pdf.addPage() and adjust the coordinates to place the second div where you need.

jsPDF - jsPDF not rendering aws image into pdf

I'm trying to convert html element into pdf. Everything is working fine , but it doesn't render the aws image in pdf.
Even though if i add a local path to the img src attribute then it render's that image in pdf.
const invoiceSection = $("#customer_invoice_bill");
let invoiceName = invoiceSection.data("invoiceName");
$("#btnDownloadInvoice").on("click", function () {
var pdfConf = {
pagesplit: true,
background: "#fff",
image: { type: "webp", quality: 0.98 }, // as per req
html2canvas: { dpi: 300, letterRendering: true, useCORS: true },
};
var pdf = new jsPDF("p", "pt", [
invoiceSection.height() + 100,
invoiceSection.width(),
]);
pdf.addHTML(invoiceSection, pdfConf, function () {
pdf.save(`${invoiceName}.pdf`);
});
});
this is the html element
https://i.stack.imgur.com/gEua9.png
this is how pdf is rendered without image. but it rendered the last image which is below(signature) as i have assign a local path to the src=""
https://i.stack.imgur.com/M3qrT.png

DropzoneJS: Image dimensions after file has been resized

I’m using DropzoneJS to facilitate the upload of files (images) as part of a .net core application. I want to be able to send the dimensions (width and height) of the file to the .net controller. But I haven’t been able to figure out how to obtain the media file dimensions after the file has been resized.
Here is the js declaration:
product_media_dropzode_ctrl = new Dropzone("#product_form_ctrl", {
paramName: "file",
maxFilesize: 3,
maxFiles: 10,
acceptedFiles: ".jpeg,.jpg,.png,.gif",
autoProcessQueue: true,
uploadMultiple: false,
parallelUploads: 100,
resizeWidth: 1920,
resizeHeight: 1080,
resizeMimeType: 'image/jpeg',
resizeQuality: 1.0,
resizeMethod: 'crop',
accept: function (file, done) {
if (UploadedNumberFiles() > maxNumberOfMediaFile) {
done("The maximum number of files has been reached.");
}
else {
done();
}
},
init: function () {
this.on("thumbnail", function (file, dataUrl) {
**console.log("widthXheight:" + file.width + "X" + file.height);**
$('#product_width_hidden_ctrl').val(file.width);
$('#product_height_hidden_ctrl').val(file.height);
})
this.on("success", function (file, response) {
HandleUploadSuccess(response);
});
this.on('error', function (file, response) {
HandleUploadError(response);
});
},
});
}
If I upload a file 4151X2000, the file will be resized to 1920X1080, however the line below will display 4151X2000 instead of 1920X1080. If I upload a file 1800X1200, 1800X1200 is display instead of 1800X1080 which is the dimension of the file after being resized
**console.log("widthXheight:" + file.width + "X" + file.height);**
Is there a way to capture the file dimensions after the image has been resized and before the file is submitted to the server?

Set camera ios phonegap

I'm having a couple of issues I tell them. I am taking a picture with the plugin Camera with phonegap and that suits me, what I do with that image is about putting another picture above I'm doing it through canvas and the problem is this left images to make it look. Zoom or something applied.
Original image
https://scontent-mia.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/10987455_403839709795958_6331116352644016645_n.jpg?oh=df8fc2cef2c1255a6babec94b3a2056d&oe=557E3520
Canvas image
https://scontent-mia.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/11064814_403839689795960_2255378534429169909_n.jpg?oh=9c0ebab7c6e9911cc28695fa80cc3033&oe=5581D16B
Also, when I take photos I can not take it whole, something I do not understand that there will be cutting the image.
Here's the setup method camera and canvas.
$scope.guardarFoto = function(){
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var img = document.getElementById("img_foto");
var img2 = document.getElementById("img_foto2");
ctx.drawImage(img,0,0);
ctx.drawImage(img2,0,0);
window.canvas2ImagePlugin.saveImageDataToLibrary(
function(msg){
console.log(msg);
},
function(err){
console.log(err);
},
document.getElementById('myCanvas')
);
}
Camera.DestinationType = {
DATA_URL : 0, // Return image as base64-encoded string
FILE_URI : 1, // Return image file URI
NATIVE_URI : 2 // Return image native URI (e.g., assets-library:// on iOS or content:// on Android)
};
Camera.PictureSourceType = {
PHOTOLIBRARY : 0,
CAMERA : 1,
SAVEDPHOTOALBUM : 2
};
Camera.EncodingType = {
JPEG : 0, // Return JPEG encoded image
PNG : 1 // Return PNG encoded image
};
Camera.MediaType = {
PICTURE: 0, // allow selection of still pictures only. DEFAULT. Will return format specified via DestinationType
VIDEO: 1, // allow selection of video only, WILL ALWAYS RETURN FILE_URI
ALLMEDIA : 2
};
Camera.Direction = {
BACK : 0, // Use the back-facing camera
FRONT : 1 // Use the front-facing camera
};
$scope.getPhoto = function() {
var options = {
quality: 100,
destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.CAMERA,
allowEdit: true,
encodingType: Camera.EncodingType.JPEG,
targetWidth: 500,
targetHeight: 500,
popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: true,
correctOrientation: true
};
Camera.getPicture(options).then(function(imageURI) {
console.log(imageURI);
$scope.lastPhoto = imageURI;
}, function(err) {
// error
});
};

Is there a way to load data into Highstock without the use of PHP?

I've been bashing my head for 3 days trying all three methods of loading data into Highstock chart. Is there a way to import data without using PHP? I'm on an IIS Server with no PHP installed.
When I use the $j.getJSON() method pointing to http://www.highcharts.com/samples/data/jsonp.php it works fine. However, when I started trying to add my own data (I tried CSV and XML) I can see everything runs without a problem via the firebug console, but I get a weird result.
http://i.imgur.com/qj5y8bM.png
Can I please get a sample (3 plot points) for a Stockchart chart. All the samples I can find are for a Highchart Barchart. Which is a completely different beast than what I'm doing with StockChart. Please, I need a sample CSV and sample JSON to base this off of. Somebody please help.
Take a look the docs, there're some demos and explanation about how to do it.
First, create the csv.
Categories,Apples,Pears,Oranges,Bananas
John,8,4,6,5
Jane,3,4,2,3
Joe,86,76,79,77
Janet,3,16,13,15
Second, define the basic chart options.
var options = {
chart: {
renderTo: 'container',
defaultSeriesType: 'column'
},
title: {
text: 'Fruit Consumption'
},
xAxis: {
categories: []
},
yAxis: {
title: {
text: 'Units'
}
},
series: []
};
Third, process data.
$.get('data.csv', function(data) {
// Split the lines
var lines = data.split('\n');
// Iterate over the lines and add categories or series
$.each(lines, function(lineNo, line) {
var items = line.split(',');
// header line containes categories
if (lineNo == 0) {
$.each(items, function(itemNo, item) {
if (itemNo > 0) options.xAxis.categories.push(item);
});
}
// the rest of the lines contain data with their name in the first position
else {
var series = {
data: []
};
$.each(items, function(itemNo, item) {
if (itemNo == 0) {
series.name = item;
} else {
series.data.push(parseFloat(item));
}
});
options.series.push(series);
}
});
// Create the chart
var chart = new Highcharts.Chart(options);
});
And here is the result.
You should be able to load data into a Highcharts/Highstock object by using an array or a named-value object. The PHP sample URL on highcharts.com returns JSON data that can be understood by Highcharts/Highstock, but you can use any data source that returns JSON data.
As an example, here is some code that uses a simple array for the series data:
$(function() {
window.chart = new Highcharts.StockChart({
chart : {
renderTo : 'container'
},
title : {
text : 'My chart'
},
series : [{
name : 'My Series',
data : [
[Date.UTC(2006, 0, 29, 0, 0, 0), 30.14],
[Date.UTC(2006, 0, 29, 1, 0, 0), 34.76],
[Date.UTC(2006, 0, 29, 2, 0, 0), 34.34],
[Date.UTC(2006, 0, 29, 3, 0, 0), 33.9]
]
}]
});
});
Here's a live sample: http://jsfiddle.net/fF9Ru/1/
If you have an existing data source, please post a sample of the data it is returning. The chart may not be rendering if the data is not properly formatted.

Resources