0

In my app I have a NSView that I use as a drawing canvas to draw some primitive shapes like rects and ellipses using NSBezierPath. Right now I need to make my canvas transparent, but I want my shapes that I draw have the same opacity as they had before. I'm curious if it's possible to do it, and if it is, how?!

EDIT:

Sorry for poor previous description!

The ultimate goal for me is to be able to draw normal saturated-color shapes on the transparent canvas. By transparent I mean totally clear if this possible. When I draw shapes my view is redrawing itself all the time, so even under these conditions I want everything to be as described above.

Any kind of help is appreciated!

Eugene Gordin
  • 4,047
  • 3
  • 47
  • 80
  • Have you seen this: http://stackoverflow.com/questions/1409285/iphone-sdk-non-transparent-subviews-in-transparent-view ? – Anastasia Apr 22 '13 at 18:23
  • I'm developing for OSX not for iOS – Eugene Gordin Apr 22 '13 at 18:26
  • 1
    Please describe what you mean by "need to make my canvas transparent". Do you want your window to be entirely transparent so you can see through it to the desktop below, and only your shapes are opaque? Or, for example, you want an opaque window with white background while you are adding shapes, but when the view prints itself, the white background should be clear instead of solid white? Or....? – NSGod Apr 22 '13 at 18:38
  • sorry for poor description, just edited the post – Eugene Gordin Apr 22 '13 at 18:47
  • 1
    It's still not clear which of the interpretations of “transparent” that NSGod has listed (or any other) is the one you're using. OK, the canvas is “transparent”, so what do I see behind it? The window background? Other windows? The wall behind my monitor? – Peter Hosey Apr 22 '13 at 22:36
  • :)))) other windows...desktop...whatever is there...I want my window be clear – Eugene Gordin Apr 22 '13 at 23:23

2 Answers2

5

To make a window transparent so that whatever's behind it shows through, set the window's backgroundColor to [NSColor clearColor] and the window's opaque to NO.

Setting the view's opaque is unnecessary because it's already NO by default, and because that doesn't do what you're looking for. The opaque property of views means something different than the opaque property of windows.

Filling with clearColor is unnecessary because all that does is erase what you've previously drawn. If you haven't drawn anything there previously, you are erasing a clean board.

Anything your views do draw (that isn't clearColor) will still be visible.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
1

set backgroundColor of NSView to transparent such as:

You can use this method

- (void)drawRect:(NSRect)dirtyRect {
    // set any NSColor for filling, say white:
    [[NSColor clearColor] setFill];
    NSRectFill(dirtyRect);
}

Reference: Best way to change the background color for an NSView

Community
  • 1
  • 1
rptwsthi
  • 10,094
  • 10
  • 68
  • 109