Code Monkey home page Code Monkey logo

Comments (7)

maoschanz avatar maoschanz commented on June 14, 2024 1

(i say that because you change the cursor callback, which i highly suspect to be currently wrong)

from drawing.

maoschanz avatar maoschanz commented on June 14, 2024

While i agree it looks better, it is not be possible currently, so this issue might stay opened for years

from drawing.

linuxr01 avatar linuxr01 commented on June 14, 2024

Is it possible now? If you start drawing it looks strange.
Drawing

from drawing.

maoschanz avatar maoschanz commented on June 14, 2024

The issue doesn't look closed

from drawing.

fabiocolacio avatar fabiocolacio commented on June 14, 2024

You can use cairo_context_translate() in your on_draw() to translate the canvas to the center. Here I added to the example I posted in issue #121.

	def on_draw(self, area, cairo_context):
		"""Signal callback. Executed when self.drawing_area is redrawn."""

		surface = self.get_surface()

		allocation = self.get_allocation()
		width = allocation.width
		height = allocation.height
		image_width = surface.get_width()
		image_height = surface.get_height()

		# Fill background with a color
		cairo_context.new_path()
		cairo_context.set_source_rgb(1, 0, 0)
		cairo_context.move_to(0, 0)
		cairo_context.rel_line_to(height, 0)
		cairo_context.rel_line_to(height, width)
		cairo_context.rel_line_to(0, width)
		cairo_context.close_path()
		cairo_context.fill()
		cairo_context.paint()

		# Draw the image on top
		cairo_context.translate((width / 2) - (image_width / 2), (height / 2) - (image_height / 2))
		cairo_context.scale(self.zoom_level, self.zoom_level)
		cairo_context.set_source_surface(surface, \
		                                 -1 * self.scroll_x, -1 * self.scroll_y)
		cairo_context.paint()

		self.active_tool().on_draw(area, cairo_context)

Then all the coordinates in the cursor callbacks need to be updated like this:

		event_x, event_y = self.get_event_coords(event)
		allocation = self.get_allocation()
		width = allocation.width
		height = allocation.height
		surface_width = self.surface.get_width()
		surface_height = self.surface.get_height()
		event_x -= (width - surface_width) / 2
		event_y -= (height - surface_height) / 2

I tested it out locally. Here are the results:

centered-canvas

Let me know if you'd like me to open a pull request. :)

from drawing.

maoschanz avatar maoschanz commented on June 14, 2024

i wanted to think about this issue once the zoom will work entirely as i expect, but if you already have a solution and you don't see any major bug when you zoom in or out, or when the image is bigger than the window, you can open a PR now

from drawing.

fabiocolacio avatar fabiocolacio commented on June 14, 2024

gotcha. I will hold off on this PR then until the callbacks are in a known working state.

from drawing.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.