image patterns as shape fill



  • Hi there Drawbotters!

    Is it possible to set the fill of a given shape as a repeating tiled image?



  • This is an old post I realize, but since I just happened to stumble upon it and it didn’t get any responses, I’ll just mention that you can use clipPath() and then looping through a grid and drawing an image.

    # make an image that we’ll tile
    im = ImageObject()
    im.sunbeamsGenerator(
        (100, 100),  # size
        (0, 0),  # center
        )
    
    # draw a single tile in the lower left, for reference
    image(im, (0, 0))
    
    # get the size 
    imWidth, imHeight = im.size()
    rows = int(ceil(height()/imHeight))
    cols = int(ceil(width()/imWidth))
    
    # make a shape
    shape = BezierPath()
    shape.oval(100, 100, 800, 800)
    shape.rect(500, 500, 400, 400)
    
    # i always like to save state before clip path
    with savedState():
        # clip the path using the shape
        clipPath(shape)
        # loop through y and x to make a grid
        for y in range(rows):
            for x in range(cols):
                # draw the image a bunch of times
                image(im, (x*imWidth, y*imHeight))
    

  • admin

    not out of the box...

    but could be a nice feature to have support patterns

    the internals could like this:

    # create a source image
    source = ImageObject()
    with source:
        size(50, 50)
        rect(0, 0, 50, 10)
        for i in range(0, 5, 2):
            fill(random(), random(), random())
            oval(i * 10, 10, 10, 40)
        
    import AppKit
    
    # create an empty image
    im = AppKit.NSImage.alloc().initWithSize_((300, 300))
    # lock focus so drawing happens inside the image
    im.lockFocus()
    # create a color with a pattern of the source image and set it as the current color
    AppKit.NSColor.colorWithPatternImage_(source._nsImage()).set()
    # draw a rectangle with that pattern color
    AppKit.NSRectFill(((0, 0), (300, 300)))
    # unlock focus
    im.unlockFocus()
    
    # back to drawbot and draw the image object somewhere
    image(im, (50, 50))
    

Log in to reply