Javis.jl icon indicating copy to clipboard operation
Javis.jl copied to clipboard

Add Functionality to Easily Add Images to J-Objects

Open TheCedarPrince opened this issue 4 years ago • 6 comments

Is your feature request related to a problem? Please explain. After some fun experiments today, I realized that we do not have a way to easily add images to J-Objects!

Describe the solution you'd like

Here is the syntax I created for a function call:

JCircle(dog_positions[i], 27; img = readpng("tiny_doggo.png"))

Then, here is what I changed in base Javis to the JCircle function:

JCircle(
    center::Point,
    radius::Real;
    color = "black",
    linewidth = 1,
    action = :stroke,
    img = nothing,
) =
    (
        args...;
        center = center,
        radius = radius,
        color = color,
        linewidth = linewidth,
        action = action,
        img = img,
    ) -> _JCircle(center, radius, color, linewidth, action, img)

and here is the internal method:

function _JCircle(center, radius, color, linewidth, action, img)
    sethue(color)
    setline(linewidth)

    if !isnothing(img)
        circle(center, radius, :stroke)
        circle(center, radius, :clip)
        placeimage(img, center, centered = true)
    else
        circle(center, radius, action)
    end

    return center
end

Describe alternatives you've considered

I considered writing an external function outside of Javis to do this, but it felt like too much work for such a small new feature addition. Then, I thought about creating a new base Javis function. That didn't make much sense as it is not much different from the base JCircle J-Object implementation. So, I added it to the base Javis JCircle definition.

What do you think @Wikunia and @Sov-trotter ? I tested out the above changes and none of the tests we had in place failed using this new functionality.

TheCedarPrince avatar Aug 11 '21 04:08 TheCedarPrince

Would it be better if we create a separate JImage and also a JCircle? Putting images into JObjects(JCircle JRect etc.) will clip the images and having a JImage would simply place the full image?

Another thought unrelated to this issue: Eg: Manim has many many MObjects. In our case we can create provide many other handy yet complex shapes using JShape in base Javis.

Sov-trotter avatar Aug 11 '21 05:08 Sov-trotter

Yes I think that makes sense. Having the functionality for all shapes shouldn't be too complicated as well. We probably want to restructure some parts to avoid writing the same code over and over.

Creating more functionality by using JShape internally sounds like a good idea too

Wikunia avatar Aug 11 '21 05:08 Wikunia

I like the idea of a JImage syntax. What are you thinking about the syntax looking like? I was imagining it a bit differently like this:

JImage(O, 30, star)

Where the function is defined like:

function JImage(center::Point, radius::Real, clipshape, img)
	clipshape(center, radius, :clip)
        placeimage(img, center, centered = true)
end

When you say that JImage simply places the first image, are you thinking about a syntax like this:

JImage(O, 30, JCircle)

Or what were you imagining/envisioning @Sov-trotter ?

TheCedarPrince avatar Aug 12 '21 04:08 TheCedarPrince

function JImage(center::Point, radius::Real, clipshape, img) clipshape(center, radius, :clip) placeimage(img, center, centered = true) end

I will be hard to generalize JImage for every shape(in case we want to clip), as we would need to write lots of multiple dispatch, all of it doing the asme thing inside, so that code can sit within respective shapes. So JImage should simply place the full image. The problem I see with this is that we will be having different functions to place images which can get confusing but specifying it in the docs should be good.

eg:

function JImage(pos::Point, img)
    placeimage(img, center, centered = true)
end

Sov-trotter avatar Aug 12 '21 04:08 Sov-trotter

We might want to have a vector of those convenience methods in the future. That way we could say [JCircle(O,50;action=:clip), JImage(filename, width, height)]

Wikunia avatar Aug 12 '21 06:08 Wikunia

Created a potential implementation of ideas here! Feel free to review that PR.

TheCedarPrince avatar Aug 14 '21 18:08 TheCedarPrince