Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tkInter Canvas Antialiasing hack! #986

Closed
Harvie opened this issue Sep 29, 2018 · 6 comments
Closed

tkInter Canvas Antialiasing hack! #986

Harvie opened this issue Sep 29, 2018 · 6 comments

Comments

@Harvie
Copy link
Collaborator

Harvie commented Sep 29, 2018

I've figured little hack to do antialiasing in tkinter canvas (which is otherwise known to not be able to do antialiasing by itself):

image

#No antialiasing
self.create_line(0,10,70,200, fill="#000", width=5)

#Antialiasing (do 0.5px thicker line with approx 33% of color intensity before rendering line)
self.create_line(10,10,80,200, fill="#AAA", width=5.5)
self.create_line(10,10,80,200, fill="#000", width=5)

5px:
image

1px:
image

It's not extremely smooth subpixel antialiasing, but i think it's much easier on eyes!
All we have to do is override tkinter Canvas methods like create_line() or create_oval() to do this.
Only problem is that we have to calculate right color somehow. That can be done like this:

                rgb = self.winfo_rgb("red")
                print rgb[0], rgb[1], rgb[2]
@Harvie Harvie changed the title Canvas Antialiasing tkInter Canvas Antialiasing hack! Sep 29, 2018
@sfinexer
Copy link
Contributor

sfinexer commented Sep 29, 2018

@Harvie And how can this be applied?
It is possible for the depth of milling, but the coordinates are already quite small.
For my purposes, I have the original b-CNC and I want to get anti-aliasing but without rgb, I need a line ... Without kinks ...

@sfinexer
Copy link
Contributor

If it's just for display, then I think it's superfluous, because you need to see what will be done.

Harvie added a commit to Harvie/bCNC that referenced this issue Sep 29, 2018
@Harvie
Copy link
Collaborator Author

Harvie commented Sep 29, 2018

@sfinexer

And how can this be applied?

i've already applied it in my commit

It is possible for the depth of milling, but the coordinates are already quite small

it does not affect milling or precision

If it's just for display, then I think it's superfluous, because you need to see what will be done.

It will not affect your ability to see details (which is quite bad in bCNC anyway). Currently the view of bCNC is far from accurate. It approximates arcs using lines, etc... But missing antialiasing is one of the reasons why you can't see lines accurately. Read something on the topic if you need. However my eyes hurt from non-antialiased lines, so i've added this, because it was simple to do.

However this was just cheap simple fix until we move to OpenGL #591

@Harvie
Copy link
Collaborator Author

Harvie commented Sep 29, 2018

Just for reference if someone is looking for Tkinter Canvas antialiasing:

ANTIALIAS_CHEAP = True

class CNCCanvas(Canvas, object):
	#Calculate arguments for antialiasing
	def antialias_args(self, args, winc=0.5, cw=2):
		nargs = {}

		#set defaults
		nargs['width'] = 1
		nargs['fill'] = "#000"

		#get original args
		for arg in args:
			nargs[arg] = args[arg]
		if nargs['width'] == 0:
			nargs['width'] = 1

		#calculate width
		nargs['width'] += winc

		#calculate color
		#cbg = self.winfo_rgb(CANVAS_COLOR)
		cbg = self.winfo_rgb(self.cget("bg"))
		cfg = list(self.winfo_rgb(nargs['fill']))
		#print cbg, cfg
		cfg[0] = (cfg[0] + cbg[0]*cw)/(cw+1)
		cfg[1] = (cfg[1] + cbg[1]*cw)/(cw+1)
		cfg[2] = (cfg[2] + cbg[2]*cw)/(cw+1)
		nargs['fill'] = '#%02x%02x%02x' % (cfg[0]/256, cfg[1]/256, cfg[2]/256)
		#nargs['fill'] = '#AAA'
		#print cfg, nargs['fill']

		return nargs

	#Override alias method if antialiasing enabled:
	if ANTIALIAS_CHEAP:
		def create_line(self, *args, **kwargs):
			nkwargs = self.antialias_args(kwargs)
			super(CNCCanvas, self).create_line(*args, **nkwargs)
			return super(CNCCanvas, self).create_line(*args, **kwargs)

@Harvie Harvie closed this as completed Sep 29, 2018
@Talhoid-Coder
Copy link

Also, tkinter canvas antialiasing is enabled on OSX.
Is there a way we can change the code to enable antialiasing in the canvas?

@Harvie
Copy link
Collaborator Author

Harvie commented Apr 21, 2021

Also, tkinter canvas antialiasing is enabled on OSX.

What are you refering to? I am not aware the tkinter canvas has antialiasing... And this hack was not huge success, especialy in terms of performance. bCNC canvas needs complete rewrite to C.

rar8000 pushed a commit to rar8000/bCNC that referenced this issue Jul 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants