<html>
<input type="file" name="Image">
</html>
<script>
fabric.Image.fromURL('image.jpg', function(img) {enter code here
var oImg = img.set({ left: 50, top: 100, angle: 00 }).scale(0.9);
canvas.add(oImg).renderAll();
canvas.setActiveObject(oImg);
});
</script>
i m trying to upload image file from the hard drive by input tag. This is my sample code. tell me how can i change this script so that i can choose the picture from input.
Here you have working example: http://jsbin.com/wecupu/2/edit
HTML
<!DOCTYPE html>
<html>
<head>
<script src="//cdnjs.cloudflare.com/ajax/libs/fabric.js/1.4.13/fabric.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<input id="file" type="file" accept="image/*">
<canvas id="canvas" width="600" height="600"></canvas>
</body>
</html>
JS
var readFile = function(e) {
var input = e.target;
var reader = new FileReader();
reader.onload = function(){
var img = document.createElement('img');
img.src = reader.result;
var image = new fabric.Image(img);
canvas.add(image);
};
reader.readAsDataURL(input.files[0]);
};
document.getElementById('file').addEventListener('change', readFile);
var canvas = new fabric.Canvas(document.getElementById('canvas'), {
backgroundColor: '#c8c8c8'
});
You just have to use FileReader to read image file into base64 format and add fabric.Image
to canvas.
However, there is limitation to file size. It varies across browsers: What is the size limit of a Base64 DataURL image?
Angular solution:
Typescript:
uploadImage(event) {
if (event.target.files.length > 0) {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = (e: any) => {
const data = e.target.result;
fabric.Image.fromURL(data, img => {
this.canvas.add(img);
this.canvas.centerObject(img);
});
};
reader.readAsDataURL(file);
}
}
HTML:
<button type="button" mat-raised-button (click)="fileInput.click()">Choose File</button>
<input hidden (change)="uploadImage($event)" #fileInput type="file" id="file">
I have four grids, and in each grid I have a button that I would like to have its own unique modal. Whenever I try using Bootstrap's modals' however, I am only getting the first button's data to show ...
I have four grids, and in each grid I have a button that I would like to have its own unique modal. Whenever I try using Bootstrap's modals' however, I am only getting the first button's data to show ...
I was wondering if it is possible to limit the Google Places API to search only for 'x' near 'landmark' where landmark is limited to landmarks only. For example: Not desired: coffee shop near ...
I was wondering if it is possible to limit the Google Places API to search only for 'x' near 'landmark' where landmark is limited to landmarks only. For example: Not desired: coffee shop near ...
How can I get native javascript AJAX to return response in JSON rather than 'text over HTML'? Description: In jquery, the following AJAX function returns JSON data, which is what we needed. JQUERY ...
How can I get native javascript AJAX to return response in JSON rather than 'text over HTML'? Description: In jquery, the following AJAX function returns JSON data, which is what we needed. JQUERY ...
I'm trying to find a simple, elegant way to create a "delete" button for a user's comments on a site I'm working on. Ideally, I'd like to register an event handler for a class called something like "...
I'm trying to find a simple, elegant way to create a "delete" button for a user's comments on a site I'm working on. Ideally, I'd like to register an event handler for a class called something like "...