question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] iOS click event issue and instanceof failing

See original GitHub issue

I have a runtime that adds charts to multiple canvases on the same page. I have a list of charts in the left column, and every time one of those items are clicked, I add a chart on the right column. I am using Vaadin in my runtime stack.

On iOS, after I add a chart, the Vaadin stack no longer receives any click events.

So I set events=[] hoping that would solve the problem. It did not.

So instead of adding canvas, I added iframes, then added canvas to the iframes, and fed that canvas to Charts.js, in hopes of isolating the click eater.

Charts.js would not accept the canvas or it’s context2d because BOTH of the instanceof checks in platform.dom.js. fail when the canvas is coming from the iframe. When I remove the type checks, Charts.js works like a charm.

	return {
		acquireContext: function(item, config) {
			if (typeof item === 'string') {
				item = document.getElementById(item);
			} else if (item.length) {
				// Support for array based queries (such as jQuery)
				item = item[0];
			}

			if (item && item.canvas) {
				// Support for any object associated to a canvas (including a context2d)
				item = item.canvas;
			}

			if (item instanceof HTMLCanvasElement) {
				// To prevent canvas fingerprinting, some add-ons undefine the getContext
				// method, for example: https://github.com/kkapsner/CanvasBlocker
				// https://github.com/chartjs/Chart.js/issues/2807
				var context = item.getContext && item.getContext('2d');
				if (context instanceof CanvasRenderingContext2D) {
					initCanvas(item, config);
					return context;
				}
			}

			return null;
		}

Environment

  • Chart.js version: 2.5
  • Browser name and version: iOS safari and chrome
  • Link to your project: n/a

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:23 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
simonbrunelcommented, Apr 19, 2017

Your else if condition includes the first one and doesn’t really mean: “the canvas is in an IFRAME” but “item is not a HTMLCanvasElement because ???”. And there are more reliable ways to detect that the canvas is contained in IFRAME (e.g. canvas.ownerDocument !== document), so basically your code could be written like that:

    var context = item && item.getContext && item.getContext('2d');
    if (context) {
        config._isInIframe = item.ownerDocument !== document;
        initCanvas(item, config);
        return context;
    }

But handling canvas in IFRAME should not become a special case across the code so I would not add this extra config._isInIframe until we really need it. Also removing the two instanceof can help to fix #3696 and #3887 😃

0reactions
etimbergcommented, Apr 22, 2017

Resolved in #4165

Read more comments on GitHub >

github_iconTop Results From Across the Web

[BUG] iOS click event issue and instanceof failing #4152
I am using Vaadin in my runtime stack. On iOS, after I add a chart, the Vaadin stack no longer receives any click...
Read more >
TypeScript instanceof not working - Stack Overflow
Sadly, I've got a case where typeof fails for objects that have been created with a constructor. Logging them will show the correct...
Read more >
instanceof - JavaScript - MDN Web Docs
The instanceof operator tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object.
Read more >
instanceof, why not? : r/javascript - Reddit
The big issue with instanceof is that it doesn't work with objects that are shared between windows/frames. People writing libraries that are meant...
Read more >
JavaScript Instanceof Operator - GeeksforGeeks
The instanceof operator in JavaScript is used to check the type of an object at run time. It returns a boolean value if...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found