As titled,
Is it possible to initialize canvas with image on it using Pixi so that i can add filter on top of it? Im using gsap 3.2.6 and Pixi 5.2.4.
I have the canvas created like this (using React btw)
useEffect(() => {
let ctx = imgDisplayer.current.getContext('2d');
let img = new Image();
img.onload = function(){
imgDisplayer.current.width = img.width; // To set canvas width to image width
imgDisplayer.current.height = img.height; // To set canvas height to image height
ctx.drawImage(img, 0, 0);
setCanvas(imgDisplayer.current);
}
img.src = source;
}, []);
That setCanvas is to store the loaded canvas into state, so that i can use it to initialize Pixi.
if (canvas){
const app = new Pixi.Application({
width: 1300,
height: 866,
backgroundColor: 0x000000,
autoResize: true,
view: canvas
});
}
Problem is, this throws me the following error
Error: This browser does not support WebGL. Try using the canvas renderer
If only i could fetch an https image (direct image link) then this wouldn't be a problem, and i can load the image using Pixi.Sprite insteaad.. but because of cross origin, i cannot think of way on how to render the image. I can render it just fine on canvas, but not with Pixi.
The error you pasted is saying that your browser does not support WebGL.
Please check WebGL support on those pages: https://get.webgl.org/ , https://webglreport.com/
Can you try to initialize Pixi on some simpler page just to check if it works for you? I mean, just create simple html page (without React etc), try to setup Pixi there and try to draw anything (some red circle etc).
Related
using react chartjs i m trying to download chart as a image.
this is my code:
const saveAsImage = () => {
const imageLink = document.createElement('a');
imageLink.download ='img.png';
imageLink.href =chartRef.current.toBase64Image('image/jpg', 1);
imageLink.click();
}
downloaded image background colour should be clear
You're using toBase64Image('image/jpg', 1) and jpg images don't have transparent backgrounds. Try with image/png.
I am building simple application, but one problem is I lost a lot of time, that's why I write here, I have images in my app that I get from another server, I need to know the original size of the image, I used to use React-native getSize() method to get the right dimensions, it worked on IOS but on Android it was calculating incorrect dimensions, this happened when the size of the image was too large, after reading a few articles and comments I analyzed that there might have been a problem in Fresco, I was unable to solve this problem.
//returns wrong dimensions
Image.getSize(myUri, (width, height) => {setState({width, height})});
const setImageSize = (imageUrl:any) => {
const img = new Image();
img.src =`https:${imageUrl}`;
img.onload = () => {
console.log(
"height-:", img.height,
"width-:", img.width
);
};
};
useEffect(()=>{
setImageSize(image)
},[])
After that I tried to write a function that would call this url and would get dimensions, but I can not create constructor function with new Image() it's throws error TypeError: Object is not a constructor (evaluating 'new _reactNativeElements.Image()'). I do not know what to do or where to find the solution. thank you
Image.getSize(myUri, (width, height) => { this.setState({ width, height }) });
Refer to this!
I am trying to create custom icons for my map markers in my mapbox in React, however essentially all of the existing documentation describes how to accomplish this when you are using a geojson wrapper to define a set amount of markers. This solution doesn't work for me, since the amount of markers for this application needs to be dynamic.
var el = document.createElement("div");
el.className = 'marker';
el.style.backgroundImage = 'url(droneIconSVG.svg)';
el.style.width = "50000px";
el.style.height = "50000px";
console.log(el.style.backgroundImage);
console.log(image01);
var drones = {};
firebase
.database()
.ref("drones")
.on("child_added", function(snapshot) {
var drone = new mapboxgl.Marker(el).setLngLat([snapshot.val().lat, snapshot.val().lng]).addTo(map);
drones[snapshot.val().id] = drone;
});
This code shows the creation of a marker, and assignment of that marker to every "drone" icon that has been added to my real time database in firebase. Any ideas as to why it might not be representing my "droneIconSVG.svg" file properly? It is a normal SVG file that I exported from illustrator.
Thank you
Can't make image to be drawn on layer when I apply blur filter.
In the parent component I add two instances of my component with konva image. One as is and one with image blur filter. First one draw correctly, the second one show no image. Interestingly if I go to another route and return to this it drawn correctly if I add the same image.
Image is added in componentDidMount:
componentDidMount() {
var img = new Konva.Image({
image: this.state.imageStateScaled,
width: 300,
height: 250
});
let st: Konva.Stage = this.konvaLayer.current.getStage();
if (this.state.blured) {
img.filters([Konva.Filters.Blur]);
img.blurRadius(30);
img.cache({ width: this.konvaLayer.current.getWidth(), height: this.konvaLayer.current.getHeight() });
}
this.konvaLayer.current.add(img);
st.draw();
}
My render function:
public render() {
let stageWithImage =
<Stage ref={this.konvaStage} width={300} height={250}>
<Layer ref={this.konvaLayer}></Layer>
</Stage>
return ({ stageWithImage })
}
It looks like it caused by konva.image.cache() function. If I put it outside if block and it is applied to both images, non is drawn first time.
Any ideas?
UPDATE
I've created a small demo with the issue
source https://github.com/CAIIIA/konvaDrawImageIssue
Live demo http://domit.co.uk/demo/index.html
I've also noticed that all browser works differently with this demo.
Chrome as I described above. One image shown, the other only after refresh.
Firefox doesn't draw at first hit only after reload
Also EDGE browser seems not to have this issue. Works like charm
!!!Doesn't work in Internet explorer for some reason at all.
You need to apply filters and cache AFTER image is loaded.
var img = new Konva.Image({
image: this.props.image,
width: 300,
height: 250
});
this.konvaLayer.current.add(img);
this.props.image.onload = () => {
if (this.props.blurred) {
img.filters([Konva.Filters.Blur]);
img.blurRadius(30);
img.cache({ width: this.konvaLayer.current.getWidth(), height: this.konvaLayer.current.getHeight() });
this.konvaLayer.current.draw();
}
}
Offtopic notes:
Try to define all Konva nodes in render function. Do not create them manually with new Konva.Something()
Do not create new Image() in render function. You may have unexpected behavior when you update your components. Take a looks here for demos: https://konvajs.github.io/docs/react/Images.html
I'm using html2canvas lib to render my html page to image, and then I put that to PDF using jspdf. But somehow my html page font is different from the font in the PDF file.
$scope.ConvertToPdf = function(obj){
$scope.downloadPdfArr = [];
$scope.downloadPdfArr = angular.copy(obj);
html2canvas($("#widget"),{
onrendered : function(canvas){
var imgData = canvas.toDataURL(
'image/png');
var doc = new jsPDF();
doc.addImage(imgData, 'PNG', 10, 10,200,100);
doc.save('sample-file.pdf');
}
})
}
This is my html page
This is the PDF I'm getting
html2canvas does not claim to handle all types of styling - if there is a font-style property on your page, perhaps it's not being handled correctly or at all. It can differ between browsers too - which browser are you using to get this result?
You can try debugging the html2canvas source in dev tools, such as the functions Font(family, size) and cloneNode(node, javascriptEnabled) to see how exactly the HTML of your page is being processed, and have a look inside the DOM node objects.