Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add image to the editor #3

Closed
emorling opened this issue Feb 20, 2021 · 6 comments
Closed

Add image to the editor #3

emorling opened this issue Feb 20, 2021 · 6 comments

Comments

@emorling
Copy link

emorling commented Feb 20, 2021

I need to add the ability to add an image to the editor, and in the future other things.

For example:

fabric.Image.fromURL('my_image.png', function(oImg) { // scale image down, and flip it, before adding it onto canvas oImg.scale(0.5).set('flipX', true); canvas.add(oImg); });

Is there a way to access the fabric object? Or should we be approaching this in another way?

@asotog
Copy link
Owner

asotog commented Feb 20, 2021

hey @emorling, do you mean the fabric canvas instance ? from this const { selectedObjects, editor, onReady } = useFabricJSEditor() then editor.canvas.add( ... )

@emorling
Copy link
Author

I am not sure I understand how to do this.

I understand that we can add an Image object using: editor.canvas.add( ... )

But how do we for example do this prior to adding it:

fabric.Image.fromURL('my_image.png',(oImg) => { });

@asotog
Copy link
Owner

asotog commented Feb 20, 2021

Ah alright you are almost there, from your project you have to regular install the fabric dependency because fabricjs-react needs it as peer dependency, then you can do exactly that:

import { fabric } from 'fabric'; // this also installed on your project
import { useFabricJSEditor } from 'fabricjs-react';

const { selectedObjects, editor, onReady } = useFabricJSEditor();
fabric.Image.fromURL('my_image.png', function(oImg) { 
    editor.canvas.add( ... )
})

You can use this boilerplate we created https://codesandbox.io/s/flamboyant-wind-ff3x8

@emorling
Copy link
Author

Fabulous! You are one of the smartest people on the planet!

BTW do you have any consulting hours this weekend, to solve this: fabricjs/fabric.js#6883

@asotog
Copy link
Owner

asotog commented Feb 20, 2021 via email

@asotog asotog closed this as completed Feb 21, 2021
@azakero
Copy link

azakero commented Oct 2, 2021

Hi. I tested the above code to render an image on the canvas, but there are multiple images being rendered. Like a million. What am I doing wrong? Here's the sandbox: https://y0rq7.csb.app/ and here's the code:

import React from "react";
import { fabric } from "fabric";
import { FabricJSCanvas, useFabricJSEditor } from "fabricjs-react";

const App = () => {
  const { selectedObjects, editor, onReady } = useFabricJSEditor();

  fabric.Image.fromURL("https://i.imgur.com/8zvUjul.jpg", function (oImg) {
    editor?.canvas.add(oImg);
  });

  return (
    <div>
      <FabricJSCanvas className="sample-canvas" onReady={onReady} />
    </div>
  );
};

export default App;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants