Skip to main content
CodeDeveloperOM
Inspiring
April 4, 2021
Question

Input File Upload img file + canvas html ?

  • April 4, 2021
  • 4 replies
  • 4388 views

 Hello programmers (^_^)

In my work, I want a Browse button to insert an image in the browser
And save them in the form ? 

The image appears at a specified width and height

by canvashtml in animate cc 

 

 

This topic has been closed for replies.

4 replies

CodeDeveloperOM
Inspiring
April 24, 2021

Doesn't work in other browsers?

CodeDeveloperOM
Inspiring
April 26, 2021

@JoãoCésar17023019  @kglad Can you solve the code problem in the browser?
I tried and did not adjust with me?

JoãoCésar17023019
Community Expert
April 19, 2021

Hi.

 

Here is an example based on the FLA you sent me:

https://drive.google.com/file/d/1pJbpzOYQh4UWQlgdzsl70c2ATcjMEjZ1/view?usp=sharing

 

JS code:

var root = this;
var inputFile;

root.main = function()
{
	root.stop();
	
	root.customBtn.addEventListener("click", function(e)
	{
		root.uploadImage({ input: inputFile, props: { type: "file", accept: "image/*" }, style: { display: "none" } }, // input
		root.libImage, // container
		function(e, container) // loadCallback
		{
			var bounds;
				
			if (container.bitmap)
				container.removeChild(container.bitmap);
			
			container.bitmap = new createjs.Bitmap(e.target.result);
								
			setTimeout(function()
			{
				container.addChild(container.bitmap);
				bounds = container.bitmap.getBounds();
						
				if (bounds.width > container.nominalBounds.width)
					container.bitmap.scaleX = container.bitmap.scaleY = bounds.width / container.nominalBounds.width;
				
				container.bitmap.x = -bounds.width * container.bitmap.scaleX * 0.5;
				container.bitmap.y = -bounds.height * container.bitmap.scaleY * 0.5;
			}, 0);
		});
	});
};

root.uploadImage = function(input, container, loadCallback)
{
	input.input = root.createElement("input", input.props, input.style);
	input.input.click();
	
	input.input.addEventListener("change", function(e)
	{
		if (e.currentTarget.files && e.currentTarget.files[0])
		{
			var reader = new FileReader();

			reader.onload = function(e)
			{
				loadCallback(e, container);
				root.removeElement(input.input);
			}
			
			reader.onerror = function(e)
			{
				root.removeElement(input.input);
				console.error("Error! Code: " + e.target.error.code);
			};

			reader.readAsDataURL(e.currentTarget.files[0]);
		}
		else
			root.removeElement(input.input);
	});
};

root.createElement = function(type, attributes, style, props)
{
	if (!props)
		props = {};
		
	var element = document.createElement(type);
	var parentNode = props.parentNode ? props.parentNode : document.body;
	var key;

	parentNode.appendChild(element);

	if (attributes)
	{
		for (key in attributes)
			element[key] = attributes[key];
	}

	if (style)
	{
		for (key in style)
			element.style[key] = style[key];
	}

	return element;
};

root.removeElement = function(element)
{	
	if (element)
		element.remove();
}

root.main();

 

I hope it helps.

 

Regards,

JC

CodeDeveloperOM
Inspiring
April 19, 2021

many thanks (^_^)
But the code works in chrome .
It does not work in Microsoft Edge .

JoãoCésar17023019
Community Expert
April 19, 2021

Edge and IE always need special attention...

 

My idea here was just to provide a template to help you get started.

 

It is very likely that you will have to make several adaptations so that everything runs according to the needs of your project.

Community Expert
April 18, 2021

Try to talk to JoãoCésar. He is a really good programmer.

 

 https://community.adobe.com/t5/user/viewprofilepage/user-id/17023019

CodeDeveloperOM
Inspiring
April 5, 2021

Any Help ?

CodeDeveloperOM
Inspiring
April 5, 2021

this.stop();
var root = this;

var imageLoader = document.getElementById("imageLoader");
this.imageLoader.addEventListener("change", handleImage.bind(this));
var canvas = document.getElementById("imageCanvas");

function handleImage(evt) {
// canvas
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext("2d");
var fileinput = document.getElementById('ab'); // input file
var img = new Image();

fileinput.onchange = function (evt) {
var files = evt.target.files; // FileList object
var file = files[0];
if (file.type.match('image.*')) {
var reader = new FileReader();
// Read in the image file as a data URL.
this.reader.readAsDataURL(file);
reader.onload = function (evt) {
if (evt.target.readyState == FileReader.DONE) {
this.img.src=evt.target.result;
this.img.onload = () => cxt.drawImage(img, 100, 100);
}
}

} else {
alert("not an image");
}
};
}