10

Eyes that follow the cursor in QGIS

 3 years ago
source link: https://hannes.enjoys.it/blog/2020/07/eyes-that-follow-the-cursor-in-qgis/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Eyes that follow the cursor in QGIS

I did this all in some random EPSG:25832 location and scale, this uses several magic numbers that make it work for that. I did not make it work for any CRS or canvas size. If you do, please share. But this is just silly fun so …..

Have two polygons for the eyes.

image-5.png

Set their Symbol Layer Type to Geometry Generator and smooth them:

smooth($geometry, 3)
image-6.png

Add another Geometry Generator symbol layer to it and throw in the following magic expression to build the pupils. It calculates the distance from your cursor to the centroids of the polygons and it prepares a line from each centroid to the cursor. Then it places a point geometry onto that line at a fraction of the distance. Use the Geometry Type “Point / MultiPoint” for this Geometry Generator.

with_variable(
  'distance',
  distance(@canvas_cursor_point, centroid($geometry)),
  with_variable(
    'line',
    make_line(centroid($geometry), @canvas_cursor_point),
    line_interpolate_point(@line, @distance/5)
  )
)

Set the layer itself to automatically refresh its rendering in the layer’s properties:

image-7-1024x827.png

Now the eyes will follow your cursor as it moves across the map canvas!

image-9.png

Task for you: The pupils are not clipped and can exit the eyes. Oops! Head over to Topi’s for a hint how to solve this: https://twitter.com/tjukanov/status/1278689814288760837

And then you paint the rest of the fucking owl:

Those are lines with 3 vertices each:

image-10.png

And the middle vertex is moved vertically using the expression below. Basically the line is reconstructed.

smooth(
  make_line(
    start_point($geometry),  -- first point is kept
    translate(
      point_n($geometry, 2),  -- second point is translated
      0,
      distance(
        @canvas_cursor_point, centroid($geometry)
      )/10  -- move according the cursor distance to centroid
    ),
    end_point($geometry)  -- last point is kept
  ),
  4
)

Here is my project file including the temporary scratch layers (use the awesome Memory Layer Saver plugin to have them loaded automatically):

This entry was posted in Uncategorized on 2020-07-03.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK