library(animation)
#setwd("D:/")

x=c(seq(0,0.5, length.out = 500),rep(0, 500),seq(0,0.5, length.out = 500),rep(0.5, 500),seq(0,0.5, length.out = 500),rep(-1,500),seq(-0.5,-1,length.out=500),rep(-0.5,500),seq(-0.5,-1,length.out=500),rep(-0.25,500),seq(-0.25,-1,length.out=500),seq(-0.5,-0.25,length.out=500))
y=c(rep(1, 500),seq(1,0, length.out = 500),rep(0, 500),seq(0,-1,length.out = 500),rep(-1,500),seq(-1,1,length.out=500),rep(1,500),seq(1,0,length.out=500),rep(0,500),seq(-0.6,-1,length.out=500),rep(-1,500),seq(0,-0.6,length.out=500))
z=rbind(x,y)
plot(y~x, xlim=c(-3,3), ylim=c(-3,3))

dev.control('enable')

myani=ani.record(reset = TRUE, replay.cur = FALSE)


a=diag(2)
x11()
for (i in seq(-3,3,length.out=100)){
  a[1,1]=i
  newmat=apply(z,2,function(x) a%*%x)
  plot(newmat[2,]~newmat[1,], xlim=c(-3,3), ylim=c(-3,3))
  ani.record()
  #Sys.sleep(1)
}

a=diag(2) 
for (i in seq(-3,3,length.out=100)){
  
  a[1,2]=i
  newmat=apply(z,2,function(x) a%*%x)
  plot(newmat[2,]~newmat[1,], xlim=c(-3,3), ylim=c(-3,3))
  ani.record()
  #Sys.sleep(1)
}

a=diag(2) 
for (i in seq(-3,3,length.out=100)){
  
  a[2,1]=i
  newmat=apply(z,2,function(x) a%*%x)
  plot(newmat[2,]~newmat[1,], xlim=c(-3,3), ylim=c(-3,3))
  ani.record()
  #Sys.sleep(1)
}

a=diag(2) 
for (i in seq(-3,3,length.out=100)){
  
  a[2,2]=i
  newmat=apply(z,2,function(x) a%*%x)
  plot(newmat[2,]~newmat[1,], xlim=c(-3,3), ylim=c(-3,3))
  ani.record()
  #Sys.sleep(1)
}

myf=function(x){matrix(c(cos(x), -sin(x), sin(x), cos(x)), byrow=TRUE, nrow=2)}

for (i in seq(0,20,length.out=100)){
  a=myf(i)
  newmat=apply(z,2,function(x) a%*%x)
  plot(newmat[2,]~newmat[1,], xlim=c(-3,3), ylim=c(-3,3))
  ani.record()
  #Sys.sleep(1)
}

saveGIF(ani.replay(), img.name = 'tmp', convert='magick', clean=TRUE)
## Output at: animation.gif
## [1] TRUE