I have an html5 canvas and one image object to save this canvas contents
(all drawings: images,draws,..).
i want to play with the order (zindex) of these 2 objects from 1 button as below:
when i want to draw, i call the canvas to the top .
when i want to save i call image to the top(full with datURL). (and rightclick to save as PNG)
These switching operations must be repetitive and reliable.
I used this code (in a function called by the button onclick) but it fails in the second time (canvas have new contents)!
var canvas = document.getElementById('ycanvas1');
// save canvas image as data url (png format by default)
var dataURL = canvas.toDataURL();
// set canvasImg image src to dataURL - it can be saved as an image
document.getElementById('canvasImg').src = dataURL;
NB: i use IE olecontrol embed on a form (working on local) .Is there any security restrictions ?
I guess in the first case:
canvas.style.display="none" // hide the canvas
img.style.display="" //show the image
in the second case:
canvas.style.display="" //show the canvas
img.style.display="none" //hide the image
Related
I want to crop an image with Google App Script if an image outside the page frame, but as far as I checked in Google App Script documentation and I could not find a way to crop the image.
pageElements.asImage().replace (imgBlob, true); it is not allowed to pass cropping dimensions as parameters in .replace() to crop a image.
i know this can be achieved using a custom API , passing the image blob and crop area that will call cropping method on another server.
But how it will be possible to work with Google App Script, looking for expert advice.
How about this answer?
Issue:
I think that in the current stage, replace(blobSource, crop) has the limitation. The official document says as follows.
crop Boolean: If true, crops the image to fit the existing image's size. Otherwise, the image is scaled and centered.
I confirmed that when the image is cropped using replace(blobSource, crop), the center of image is left. It seems that this is the current specification. And although there is the "cropProperties" of "UpdateImagePropertiesRequest" in Slides API, unfortunately, in the current stage, this cannot be still used. This has already been reported. Ref
Sample script:
If you use replace(blobSource, crop) under the current specification, how about the following sample script? As the sample situation, 2 images of "image1" and "image2" are prepared in the 1st slide, and "image1" is cropped using "image2".
The flow of this script is as follows.
Flow:
Retrieve 2 images from a slide on Google Slides.
Crop "image1" using "image2". By this, "image2" is replaced with "image1".
Move the cropped image to "image1".
Remove the original "image1".
Script:
function myFunction() {
// 1. Retrieve 2 images from a slide on Google Slides.
var slide = SlidesApp.getActivePresentation().getSlides()[0];
var images = slide.getImages();
var image1 = images[0]; // Red image.
var image2 = images[1]; // Blue image.
// 2. Crop "image1" using "image2". By this, "image2" is replaced with "image1".
var replacedImage = image2.replace(image1.getBlob(), true);
// 3. Move the cropped image to "image1".
replacedImage.setTop(image1.getTop()).setLeft(image1.getLeft());
// 4. Remove the original "image1".
image1.remove();
}
Result:
When the script is run, "image1" is cropped. But it is found that in the current stage, the center of "image1" is left by the crop.
Note:
Slides API and Slides Service are growing now. So I think that this situation might be changed by the future update. But if you want this soon, how about requesting this to the issue tracker as the future request? Ref
References:
replace(blobSource, crop)
CropProperties
Added:
At an additional sample script for using replace(blobSource, crop), I would like to propose the method for using the self image. In this sample script, when the image is sticked out, the image of out of page is removed by cropping. The basic method is the same with above sample script.
Sample script:
function myFunction() {
var s = SlidesApp.getActivePresentation();
var slide = s.getSlides()[0];
var images = slide.getImages();
var image = images[0];
var pageWidth = s.getPageWidth();
var imagePosition = image.getLeft();
var imageWidth = image.getWidth();
var check = imagePosition + imageWidth - pageWidth;
if (check > 0 && check < imageWidth) {
image
.duplicate()
.setWidth(pageWidth - imagePosition)
.asImage()
.replace(image.getBlob(), true);
image.remove();
}
}
Result:
Note:
In this sample script, as a simple sample, I prepared only the right side of the horizontal direction. So when you want to remove the vertical direction, please modify the script for your actual situation.
I am desperately searching for a good cropping tool. There are a bunch out there, for example:
Croppic
Cropit
Jcrop
The most important thing that I am trying to find is a cropping tool, that crops images without making the cropped image low in resolution. You can hack this by using the canvas tag by resizing the image. This way the image itself stays native, only the representation is smaller.
DarkroomJS was also something near the solution, but, unfortunately, the downloaded demo did not work. I'll try to figure out whats wrong. Does someone know some great alternatives, or how to get the cropped images in...let's say "native" resolution?
Thanks in advance!
You are relying on the cropping tool to provide an interface for the users. the problem is that the image returned is sized to the interface and not the original image. Rather than me sifting through the various API's to see if they provide some way of controlling this behaviour (I assume at least some of them would) and because it is such a simple procedure I will show how to crop the image manually.
To use JCrop as an example
Jcrop provides various events for cropstart, cropmove, cropend... You can add a listener to listen to these events and keep a copy of the current cropping interface state
var currentCrop;
jQuery('#target').on('cropstart cropmove cropend',function(e,s,crop){
currentCrop = crop;
}
I don't know where you have set the interface size and I am assuming the events return the crop details at the interface scale
var interfaceSize = { //you will have to work this out
w : ?,
h : ?.
}
Your original image
var myImage = new Image(); // Assume you know how to load
So when the crop button is clicked you can create the new image by scaling the crop details back to the original image size, creating a canvas at the cropped size, drawing the image so that the cropped area is corectly positioned and returning the canvas as is or as a new image.
// image = image to crop
// crop = the current cropping region
// interfaceSize = the size of the full image in the interface
// returns a new cropped image at full res
function myCrop(image,crop,interfaceSize){
var scaleX = image.width / interfaceSize.w; // get x scale
var scaleY = image.height / interfaceSize.h; // get y scale
// get full res crop region. rounding to pixels
var x = Math.round(crop.x * scaleX);
var y = Math.round(crop.y * scaleY);
var w = Math.round(crop.w * scaleX);
var h = Math.round(crop.h * scaleY);
// Assume crop will never pad
// create an drawable image
var croppedImage = document.createElement("canvas");
croppedImage.width = w;
croppedImage.height = h;
var ctx = croppedImage.getContext("2d");
// draw the image offset so the it is correctly cropped
ctx.drawImage(image,-x,-y);
return croppedImage
}
You then only need to call this function when the crop button is clicked
var croppedImage;
myButtonElement.onclick = function(){
if(currentCrop !== undefined){ // ensure that there is a selected crop
croppedImage = myCrop(myImage,currentCrop,interfaceSize);
}
}
You can convert the image to a dataURL for download, and upload via
imageData = croppedImage.toDataURL(mimeType,quality) // quality is optional and only for "image/jpeg" images
I'm able to create/draw chart with jqplot. When I right-click on it and save it as PNG, it is saving a blank image.
Here is the image data:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbkAAAEeCAYAAAAXTWt+AAACAElEQVR4nO3BMQEAAADCoPVPbQ0PoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJ4MtD8AAaRK8nUAAAAASUVORK5CYII=
Could anybody let me know is this image data is good and can I save it?
Thanks a million!
-Parameshwar
jqPlot can convert it's chart to a image with the function jqplotToImageStr()
You have to run a JavaScript to convert the dynamic chart to a savable image.
See the answers here:
JqPlot As Image
I render images from graphs without any problem...
var imgData = $('#chart').jqplotToImageStr({});
Then I send to a php page the imgData var (that contains the base64 image) to let it be downloaded directly.
I you want just to show that image, you just have to add that var to the src attr of a image tag (or opening it as a popup passing the var as the href attr of the window)
window.open(imgData,"_blank","scrollbars=0");
Anyway, if you pass that base64 code to a php page, you can download it using that code (and appropriate headers):
$data = $_POST['data']; //your base64 code
$data = substr($data,22); //remove some unuseful chars
$img=base64_decode($data); //decode data
$out = "../tmp/report_".date("d-m-Y_His").".png"; //set output image location
file_put_contents($out, $img); //create file
echo $out; //let user direct download it
I am working on a mini-app in Adobe Flash/ActionScript 3 in which I have to change the color of some objects on the scene by clicking a set of buttons. For the same objects I have to set some patterns (using images).
I can handle the change of the color, but I do not know how to put an image as background.
How can it be done?
Thank you!
You could use Graphics to draw the image.
// Draw the background
var shape:Shape = new Shape();
shape.graphics.beginBitmapFill(myBitmapData);
shape.drawRect(0, 0, myBitmapData.width, myBitmapData.height);
shape.endFill();
// Reference for later
var background:DisplayObject = shape;
You could also use Bitmap to display the image.
// Display the image
var bitmap:Bitmap = new Bitmap(myBitmapData);
// Reference for later
var background:DisplayObject = bitmap;
Now you can add this background to your object.
// Add the background at the lowest depth level
myObject.addChildAt(background, 0);
Ok, so I'm doing an html5 canvas game, and I need to draw resized images all the time (it's all pixel-art). Unfortunately, doing the resizing on drawImage makes current browsers quite sluggish, so I'm trying to do the resizing on load, and then just draw the pre-resized image.
I've tried to draw the resized images to a hidden context and then do a ctx.getImageData, but then I'm stuck with a byte array and there's no way to convert to an image. I can do a putImageData to push it to the final context, but that's slow and I apparently lose the alpha channel.
Another option could be to pre-scale things in the server, but I'd like to avoid that if at all possible.
Any ideas?
There is a method on the canvas object (not the context) called toDataURL(string mimeType), this will convert the canvas contents to a base64-encoded binary string of an image. You can use this as the src attribute of any image element.
ctx.drawImage(originalImage, 0, 0, originalImage.width, originalImage.height, 0, 0, 200, 200);
var scaledImage = new Image();
scaledImage.onload = ...;
scaledImage.src = canvas.toDataURL("image/png");
Now you can draw your scaled image normaly.