Reputation: 776
I actually have no idea with this does not work!
library(grid)
library(gridSVG)
grid.newpage()
vp <- viewport(width=0.7, height=0.7)
pushViewport(vp)
grid.rect(x = unit(0.5, "npc"), y = unit(0.5, "npc"),
width = unit(0.5, "npc"), height = unit(0.5, "npc"),
just = "centre", hjust = NULL, vjust = NULL,
default.units = "npc", name = "grid.rect.1",
gp=gpar(), draw = TRUE)
this works:
grid.garnish("grid.rect.1",onmousedown="alert('alert 1!')","pointer-events"="all")
grid.export("gridSVG1.svg")
this doesn't work
grid.garnish("grid.rect.1", onmouseover="allwhite()", redraw = TRUE)
grid.script("allwhite = function() {
anobject = document.getElementById('grid.rect.1.1');
anobject.setAttribute('style', 'fill:red');
}", name="allwhite")
grid.export("gridSVG2.svg") # saved to your current working directory
What I'm trying to achieve, is that the rectangle is filled with red if I'm hovering over the rectangle with my mouse.
Due to the fact, that this is my first encounter with js, I have to ask maybe this silly question. I'm aware of the fact, that I name the spline "grid.rect.1" but use "grid.rect.1.1" in the script part. This is just a matter of despair, due to the fact, that I realize that rect has this id, if I inspect the SVG object with Google Chrome.
I'm using Google Chrome (36.0.1985.125 m) to watch the SVG object and it seems that a somewhat simpler thing works
As always, any hint is appreciated.
Upvotes: 2
Views: 276
Reputation: 44604
I looks like you can't add attributes, so your rect
needs a fill gpar
. You'll also need to fix the id of the rect
element. If you look at the source, you'll see that "grid.rect.1.1" is actually the grouping tag.
library(grid)
library(gridSVG)
grid.newpage()
vp <- viewport(width=0.7, height=0.7)
pushViewport(vp)
grid.rect(x = unit(0.5, "npc"), y = unit(0.5, "npc"),
width = unit(0.5, "npc"), height = unit(0.5, "npc"),
just = "centre", hjust = NULL, vjust = NULL,
default.units = "npc", name = "grid.rect.1",
gp=gpar(fill='white'), # add fill parameter
draw = TRUE)
grid.garnish("grid.rect.1", onmouseover="allwhite()", `pointer-events`='all')
grid.script("allwhite = function() {
anobject = document.getElementById('grid.rect.1.1.1');
anobject.setAttribute('style', 'fill:red');
}", name="allwhite")
grid.export("gridSVG2.svg")
Upvotes: 2