]> git.openstreetmap.org Git - rails.git/commitdiff
Gif animation prototype
authormmd-osm <mmd.osm@gmail.com>
Tue, 2 Apr 2019 16:12:58 +0000 (18:12 +0200)
committermmd-osm <mmd.osm@gmail.com>
Tue, 2 Apr 2019 16:12:58 +0000 (18:12 +0200)
Gemfile
Gemfile.lock
lib/gpx.rb

diff --git a/Gemfile b/Gemfile
index ce37b7c440e4d3cb402339bb68a27cb293e014d9..5f040f95ccdc07422d866c6aee144aa6829f61cb 100644 (file)
--- a/Gemfile
+++ b/Gemfile
@@ -117,7 +117,7 @@ gem "canonical-rails"
 gem "logstasher"
 
 # Used to generate images for traces
-gem "gd2-ffij"
+gem "gd2-ffij", :git => 'git@github.com:mmd-osm/gd2-ffij.git', :branch => 'animated_gif'
 
 # Used for browser detection
 gem "browser"
index 32496e129ba67fbba417a796d355f64e28e60cd9..ffa3cd93717ae5abaf4f3b146e22bfa325c69f30 100644 (file)
@@ -1,3 +1,11 @@
+GIT
+  remote: git@github.com:mmd-osm/gd2-ffij.git
+  revision: c92057a8f699a36b5f6d208db3d11eb3bae185dd
+  branch: animated_gif
+  specs:
+    gd2-ffij (0.3.1)
+      ffi (>= 1.0.0)
+
 GEM
   remote: https://rubygems.org/
   specs:
@@ -173,8 +181,6 @@ GEM
       multipart-post (>= 1.2, < 3)
     ffi (1.10.0)
     fspath (3.1.0)
-    gd2-ffij (0.3.0)
-      ffi (>= 1.0.0)
     geoip (1.6.4)
     globalid (0.4.2)
       activesupport (>= 4.2.0)
@@ -462,7 +468,7 @@ DEPENDENCIES
   factory_bot_rails
   fakefs
   faraday
-  gd2-ffij
+  gd2-ffij!
   geoip
   htmlentities
   http_accept_language (~> 2.0.0)
@@ -512,4 +518,4 @@ DEPENDENCIES
   webmock
 
 BUNDLED WITH
-   1.16.2
+   1.16.6
index 721e5608b5cd9e22d59f2b13ab0a9965b20d51d7..57b623362eda4f173cc343c528f9b62892ab7f35 100644 (file)
@@ -48,46 +48,66 @@ module GPX
     end
 
     def picture(min_lat, min_lon, max_lat, max_lon, _num_points)
-      frames = 10
+      nframes = 10
       width = 250
       height = 250
+      delay = 50
+
+      ptsper = _num_points / nframes;
+
       proj = OSM::Mercator.new(min_lat, min_lon, max_lat, max_lon, width, height)
 
-      # TODO: create animated gif
-      # https://github.com/openstreetmap/openstreetmap-website/issues/281
-      image = GD2::Image::IndexedColor.new(width, height)
+      frames = Array.new(nframes,  GD2::Image::IndexedColor.new(width, height))
 
-      black = image.palette.allocate(GD2::Color[0, 0, 0])
-      white = image.palette.allocate(GD2::Color[255, 255, 255])
+      (0..nframes - 1).each do |n|
+        frames[n] = GD2::Image::IndexedColor.new(width, height)
+        black = frames[n].palette.allocate(GD2::Color[0, 0, 0])
+        white = frames[n].palette.allocate(GD2::Color[255, 255, 255])
+        grey = frames[n].palette.allocate(GD2::Color[187, 187, 187])
 
-      image.draw do |pen|
-        pen.color = white
-        pen.rectangle(0, 0, width, height, true)
-      end
+        frames[n].draw do |pen|
+          pen.color = white
+          pen.rectangle(0, 0, width, height, true)
+        end
 
-      image.draw do |pen|
-        pen.color = black
-        pen.anti_aliasing = true
-        pen.dont_blend = false
+        frames[n].draw do |pen|
+          pen.color = black
+          pen.anti_aliasing = true
+          pen.dont_blend = false
 
-        oldpx = 0.0
-        oldpy = 0.0
+          oldpx = 0.0
+          oldpy = 0.0
 
-        first = true
+          first = true
 
-        points do |p|
-          px = proj.x(p.longitude)
-          py = proj.y(p.latitude)
+          points.each_with_index do |p, pt|
+            px = proj.x(p.longitude)
+            py = proj.y(p.latitude)
 
-          pen.line(px, py, oldpx, oldpy) unless first
+            if ((pt >= (ptsper * n)) && (pt <= (ptsper * (n+1))))
+              pen.thickness=(3)
+              pen.color = black
+            else
+              pen.thickness=(1)
+              pen.color = grey
+            end
 
-          first = false
-          oldpy = py
-          oldpx = px
+            pen.line(px, py, oldpx, oldpy) unless first
+              first = false
+              oldpy = py
+              oldpx = px
+          end
         end
       end
 
-      image.gif
+      res = GD2::AnimatedGif::gif_anim_begin(frames[0])
+      res << GD2::AnimatedGif::gif_anim_add(frames[0], nil, delay)
+      (0..nframes - 1).each do |n|
+        res << GD2::AnimatedGif::gif_anim_add(frames[n], frames[n-1], delay)
+      end
+      res << GD2::AnimatedGif::gif_anim_end()
+
+      res
     end
 
     def icon(min_lat, min_lon, max_lat, max_lon)