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

Callback on Drawing Complete #370

Closed
finbar123 opened this issue Jun 7, 2019 · 3 comments
Closed

Callback on Drawing Complete #370

finbar123 opened this issue Jun 7, 2019 · 3 comments

Comments

@finbar123
Copy link

Hi
Excellent library,

Using konva-react we have a use case where we need to render a konva stage so that we can take a base64 image for use in further processing. But our call to grab the image is returning only a partial image.

is there a way we can check that drawing prrcess is complete before we call toDataURL ?

Basically we change state on the parent to force a render, pass back the ref in componentDidMount, call toDataURL(callbackFunction) wait for the callback and then reset the state to remove the react canvas

the stage method toDataURL(callbackFunction) works great , but we are calling it effectively from componentDidMount() , and I don't believe that the drawing is completed at that point. ( Our image is only partly captured, if we add a 2 second wait it will be the correct complete image)

Thanks

Finbar

@lavrton
Copy link
Member

lavrton commented Jun 7, 2019

It is simple to force redraw if a stage has no images (so you don't have to wait for the network requests).

stage.draw();
const url = stage.toDataURL();

But it gets tricky if you have images, so you have to wait until all components are loaded. That should be implemented in the component.

I was using this simple code for managing loading:

let loadListeners = [];

// how many processes are waiting for loading something
let loadingResourcesNumber = 0;

export function increaseLoadingCounter() {
  loadingResourcesNumber += 1;
}

export function decreaseLoadingCounter() {
  loadingResourcesNumber -= 1;
  if (loadingResourcesNumber === 0) {
    loadListeners.forEach(callback => {
      callback();
    })
    loadListeners = [];
  }
}

export function isLoading() {
  return loadingResourcesNumber > 0;
}

const TIMEOUT = 20 * 1000;
const STEP_TIMEOUT = 5;
const ATTEMPTS = TIMEOUT / STEP_TIMEOUT;

export function whenLoadedCallback(callback, attempts = ATTEMPTS) {
  loadListeners.push(callback);
}

export function whenLoaded() {
  if (!isLoading()) {
    return Promise.resolve();
  }
  return new Promise(resolve => {
    var timeout = setTimeout(() => {
      logger.error(new Error('Waiting for all components loading is failed.'));
      resolve();
    },TIMEOUT)
    
    whenLoadedCallback(() => {
      clearTimeout(timeout);
      resolve();
    });
  });
}

@lavrton lavrton closed this as completed Jun 7, 2019
@lavrton lavrton reopened this Jun 7, 2019
@finbar123
Copy link
Author

ok thanks for the quick response I think you are saying that the image loading in my component which is using use-image needs to be modified to provide a call back when all the instances have loaded ?

I can have a look and see how we can do that

Kind Regards

Finbar

@lavrton
Copy link
Member

lavrton commented Jun 9, 2019

I think you are saying that the image loading in my component which is using use-image needs to be modified to provide a call back when all the instances have loaded ?

Exactly.

@lavrton lavrton closed this as completed Jun 18, 2019
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

2 participants