function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Josip Juric87Josip Juric87 

Lightning component re-create canvas element

I have the following situation: 
There is a lightning component which shows a canvas html-element, and is used to render a chart. For some reason (which I will not explain here), when I want to redraw the chart, I need to:
1) destroy the existing canvas element,
and
2) create a new canvas element instead.
How can I do this with Lightning Components? Do I need to use the LC Renderer ?
Best Answer chosen by Josip Juric87
Josip Juric87Josip Juric87
I have found the solution:
1) Create a new canvas element using the $A.createComponent method
2) Remove the old canvas and add the new canvas to the container element, by setting it's v.body property

Here's the code:
$A.createComponent(
	'aura:html',
	{ 
		tag : 'canvas',
		HTMLAttributes : {
			width: '300',
			height: '300'
		}
	},
	function (el, status, errorMessage) {
		var container = cmp.find('container');
		container.set('v.body', [ el ]);
	}
);