Getting ControlID from WebGraphics object

Is there a way to get the ControlID of a WebCanvas control directly from its Graphics property?

E.g.

cid = g.ParentCanvas.ControlID

Currently it seems that ParentCanvas is a protected property of WebGraphics.

No.

[quote=231771:@Alwyn Bester]Is there a way to get the ControlID of a WebCanvas control directly from its Graphics property?

E.g.

cid = g.ParentCanvas.ControlID

Currently it seems that ParentCanvas is a protected property of WebGraphics.[/quote]

Not sure what you are after. I can only suppose this is related to your SVG project. Never heard of “ParentCanvas”. Since g is an object property local to Paint, how could you have forgotten where it came from ?

Jokes aside, why not do me.controlID in Paint ?

The forgetfulness must be an age thing :wink:

Yip.

Currently the DrawSVG method signature looks like follow:

Sub DrawSVG(Extends g As WebGraphics, canvas As WebCanvas, svg As String, x As Integer, y As Integer)

The goal is to change the method signature to the following:

Sub DrawSVG(Extends g As WebGraphics, svg As String, x As Integer, y As Integer)

But there is no way for me to get the canvas control id from the g paramater alone, hence the need to pass in the extra canvas parameter so that DrawSVG can determine which HTML element to paint to.

My goal is to make it as easy as possible for the caller, so that he/she can use the method the same way they would use the DrawPicture method without having to add extra logic on their side with each call (e.g. not having to determine the ControlID for themselves).

[quote=231795:@Alwyn Bester]Currently the DrawSVG method signature looks like follow:

Sub DrawSVG(Extends g As WebGraphics, canvas As WebCanvas, svg As String, x As Integer, y As Integer)
The goal is to change the method signature to the following:

Sub DrawSVG(Extends g As WebGraphics, svg As String, x As Integer, y As Integer)[/quote]

You are calling that method from within Paint ? Is that it ?

Yes, that is correct, like so…

Sub Paint(g as WebGraphics) 
  g.DrawSVG Me, SvgXML, 100, 100
End Sub

where the goal is to rather call it like so…

Sub Paint(g as WebGraphics) 
  g.DrawSVG SvgXML, 100, 100
End Sub

[quote=231802:@Alwyn Bester]Yes, that is correct, like so…

Sub Paint(g as WebGraphics) 
  g.DrawSVG Me, SvgXML, 100, 100
End Sub

where the goal is to rather call it like so…

Sub Paint(g as WebGraphics) g.DrawSVG SvgXML, 100, 100 End Sub [/quote]

Greg already answered, there is no way to get to the parent from g. As far as I know, the g object does not exist in the DOM, so even in JavaScript, there is no way to identify it’s class.

If all you need is the ControlID, why not pass it as string instead of the whole canvas ?

Originally I did pass it as a string, but then decided to rather pass the canvas because it is less code for the caller:

With string:

g.DrawSVG Me.ControlID, SvgXML, 100, 100

With canvas:

g.DrawSVG Me, SvgXML, 100, 100

If I may ask, why do you need the ControlID ? Are you doing some JavaScript magic or is it just a matter of identification ?

Using some JavaScript to tap into the browser’s SVG renderer.

To help the JavaScript to render the SVG XML to a specific canvas I need to identify the canvas, hence the need to determine the ControlID so that I can pass that ID to the JavaScript.

[quote=231837:@Alwyn Bester]Using some JavaScript to tap into the browser’s SVG renderer.

To help the JavaScript to render the SVG XML to a specific canvas I need to identify the canvas, hence the need to determine the ControlID so that I can pass that ID to the JavaScript.[/quote]

I think you are fine with your present code. If there was another way, Greg would have told.

I’m a little concerned about your code and how it interacts with our framework though. How does your code work if mixed with other drawing commands?

Haven’t done any testing yet so cannot say forsure. The code gets the canvas context and uses the “DrawImage” method of the context to draw the SVG on the canvas (Code given below).

Could this cause issues with the existing drawing commands?

Sub DrawSVG(Extends g As WebGraphics, canvas As WebCanvas, svg As String, x As Integer, y As Integer)
  Dim drawScript() As String
  Dim canvasID As String
  
  canvasID = canvas.ControlID + "_canvas"
  
  drawScript.Append("var canvas = document.getElementById('" + canvasID + "');")
  drawScript.Append("var ctx = canvas.getContext('2d');")
  drawScript.Append("var svg = '" +  svg + "';")
  drawScript.Append("var svgSrc = 'data:image/svg+xml;base64,' + window.btoa(svg);")
  drawScript.Append("var img = new Image();")
  drawScript.Append("img.src = svgSrc;")
  drawScript.Append("img.onload = function() {")
  drawScript.Append("ctx.drawImage(img, " + Str(x) + ", " + Str(y) + ");")
  drawScript.Append("}")
  
  Canvas.Page.ExecuteJavaScript(Join(drawScript, ""))
End Sub

Yup. If you want to do this, I suggest creating your own canvas element using the WebSDK. Sending your code directly to the canvas like this will likely cause lots of extra refreshes and interfere with the drawing queue.

Ok, thanks for the heads up Greg.