• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Live wallpaper from javascript

I'm trying to learn how to develop a live wallpaper but i can't understand out how to convert some code parts from javascript to kotlil

I can't understand how to convert
this.draw = draw
this.go = go

Javascript

JavaScript:
package xxxxxx

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import kotlin.math.sqrt

class Worm(var nx: Double, var ny: Double) {
    var x = 0.0
    var lastX = 0.0
    var y = 0.0
    var lastY = 0.0
    var vx = 0.0
    var vy = 0.0
    var height = 600.0
    var width = 600.0
    var speed = 3.0
    var influence = 5.0
    var random = 0.5
    var numberAngles = 8
    var path = Path()
    var paint = Paint()
    var canvas = Canvas()

    init {
        this.x = this.nx
        this.lastX = this.nx
        this.y = this.ny
        this.lastY = this.ny
        this.vx = Math.random() - 0.5
        this.vy = Math.random() - 0.5
    }

    fun draw() {
        paint.isAntiAlias = true
        paint.color = Color.parseColor("#00ff00")
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 10f

        path.moveTo(this.x.toFloat(), this.y.toFloat())
        path.lineTo(this.lastX.toFloat(), this.lastY.toFloat());
        canvas?.drawPath(path, paint)
    }

    fun go() {
        this.lastX = this.x
        this.lastY = this.y
        var resultX = 0
        var resultY = 0
        var x = this.x.toInt()
        var y = this.y.toInt()

        this.vx += resultX * influence
        this.vy += resultY * influence
        this.vx += (Math.random() - 0.5) * random
        this.vy += (Math.random() - 0.5) * random

        var som = sqrt((this.vx * this.vx) + (this.vy * this.vy))
        this.vx /= som
        this.vy /= som

        if ((x < 0)) {
            this.vx *= -1
            this.x = 0.0
        }
        if ((x > width)) {
            this.vx *= -1
            this.x = width
        }
        if ((y < 0)) {
            this.vy *= -1
            this.y = 0.0
        }
        if ((y > height)) {
            this.vy *= -1
            this.y = height
        }
        this.x += this.vx * speed * 1.5
        this.y += this.vy * speed * 1.5
    }
}

Kotlin

LiveWallpaperService.kt
Code:
package xxxxxx

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import kotlin.math.sqrt

class Worm(var nx: Double, var ny: Double) {
    var x = 0.0
    var lastX = 0.0
    var y = 0.0
    var lastY = 0.0
    var vx = 0.0
    var vy = 0.0
    var height = 600.0
    var width = 600.0
    var speed = 3.0
    var influence = 5.0
    var random = 0.5
    var numberAngles = 8
    var path = Path()
    var paint = Paint()
    var canvas = Canvas()

    init {
        this.x = this.nx
        this.lastX = this.nx
        this.y = this.ny
        this.lastY = this.ny
        this.vx = Math.random() - 0.5
        this.vy = Math.random() - 0.5
    }

    fun draw() {
        paint.isAntiAlias = true
        paint.color = Color.parseColor("#00ff00")
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 10f

        path.moveTo(this.x.toFloat(), this.y.toFloat())
        path.lineTo(this.lastX.toFloat(), this.lastY.toFloat());
        canvas?.drawPath(path, paint)
    }

    fun go() {
        this.lastX = this.x
        this.lastY = this.y
        var resultX = 0
        var resultY = 0
        var x = this.x.toInt()
        var y = this.y.toInt()

        this.vx += resultX * influence
        this.vy += resultY * influence
        this.vx += (Math.random() - 0.5) * random
        this.vy += (Math.random() - 0.5) * random

        var som = sqrt((this.vx * this.vx) + (this.vy * this.vy))
        this.vx /= som
        this.vy /= som

        if ((x < 0)) {
            this.vx *= -1
            this.x = 0.0
        }
        if ((x > width)) {
            this.vx *= -1
            this.x = width
        }
        if ((y < 0)) {
            this.vy *= -1
            this.y = 0.0
        }
        if ((y > height)) {
            this.vy *= -1
            this.y = height
        }
        this.x += this.vx * speed * 1.5
        this.y += this.vy * speed * 1.5
    }
}

Worm.kt
Code:
package xxxxxx

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import kotlin.math.sqrt

class Worm(var nx: Double, var ny: Double) {
   var x = 0.0
   var lastX = 0.0
   var y = 0.0
   var lastY = 0.0
   var vx = 0.0
   var vy = 0.0
   var height = 600.0
   var width = 600.0
   var speed = 3.0
   var influence = 5.0
   var random = 0.5
   var numberAngles = 8
   var path = Path()
   var paint = Paint()
   var canvas = Canvas()

   init {
       this.x = this.nx
       this.lastX = this.nx
       this.y = this.ny
       this.lastY = this.ny
       this.vx = Math.random() - 0.5
       this.vy = Math.random() - 0.5
   }

   fun draw() {
       paint.isAntiAlias = true
       paint.color = Color.parseColor("#00ff00")
       paint.style = Paint.Style.STROKE
       paint.strokeWidth = 10f

       path.moveTo(this.x.toFloat(), this.y.toFloat())
       path.lineTo(this.lastX.toFloat(), this.lastY.toFloat());
       canvas?.drawPath(path, paint)
   }

   fun go() {
       this.lastX = this.x
       this.lastY = this.y
       var resultX = 0
       var resultY = 0
       var x = this.x.toInt()
       var y = this.y.toInt()

       this.vx += resultX * influence
       this.vy += resultY * influence
       this.vx += (Math.random() - 0.5) * random
       this.vy += (Math.random() - 0.5) * random

       var som = sqrt((this.vx * this.vx) + (this.vy * this.vy))
       this.vx /= som
       this.vy /= som

       if ((x < 0)) {
           this.vx *= -1
           this.x = 0.0
       }
       if ((x > width)) {
           this.vx *= -1
           this.x = width
       }
       if ((y < 0)) {
           this.vy *= -1
           this.y = 0.0
       }
       if ((y > height)) {
           this.vy *= -1
           this.y = height
       }
       this.x += this.vx * speed * 1.5
       this.y += this.vy * speed * 1.5
   }
}
 
Back
Top Bottom