Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8976033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:01:38+00:00 2026-06-15T19:01:38+00:00

In my first canvas attempt (a classic slot machine), the performance is terrible. After

  • 0

In my first canvas attempt (a classic slot machine), the performance is terrible. After the first few spins, Chrome nearly crashes.

Is it the use of prototype?

Stack wants me to write a lot more text here to submit, so I will.

var Renderer = function(options) {
    var self = this
    _.extend(self, options)
    self.positions = _.map(self.reels, function(r) { return 0 })
    self.heights = _.map(self.reels, function(r) { return r.length * self.symbolHeight })
    self.context = self.canvas.getContext('2d')

    self.render()
}

Renderer.prototype.render = function() {
    var self = this

    self.context.drawImage(self.bg, 0, 0)

    self.context.rect(0, 0, self.symbolWidth * self.rows, self.symbolHeight * self.reels.length)
    self.context.clip()

    for (var reel = 0; reel < self.reels.length; reel++) {
        for (var row = 0; row <= self.rows; row++) {
            var reelIndex = Math.floor(self.positions[reel] / self.symbolHeight) + row
            , symbolOffset = self.positions[reel] % self.symbolHeight

            if (reelIndex >= self.reels[reel].length) {
                reelIndex -= self.reels[reel].length
            }

            var symbolIndex = self.reels[reel][reelIndex]
            , x = reel * self.symbolWidth
            , y = row * self.symbolHeight - symbolOffset

            /*
            console.log('drawing symbol', symbolIndex, 'from', self.symbolDims[symbolIndex].x,
                self.symbolDims[symbolIndex].y, 'to', x, y)
            */

            self.context.drawImage(
                self.symbols,
                self.symbolDims[symbolIndex].x,
                self.symbolDims[symbolIndex].y,
                self.symbolDims[symbolIndex].width,
                self.symbolDims[symbolIndex].height,
                x,
                y,
                self.symbolWidth,
                self.symbolHeight
            )
        }
    }
}

Renderer.prototype.spinupTick = function() {
    var allReelsAtMaxSpeed = true
    , maxReelSpeed = 20
    , self = this
    , acc = 1
    , reel

    for (reel = 0; reel < this.reels.length; reel++) {
        if (!self.reelsMoving[reel]) continue

        if (self.speeds[reel] < maxReelSpeed) {
            if (Math.random() < 0.5) {
                self.speeds[reel] += acc
            }

            allReelsAtMaxSpeed = false
        }

        self.positions[reel] -= self.speeds[reel]

        if (self.positions[reel] < 0) {
            self.positions[reel] += self.heights[reel]
        }
    }

    if (allReelsAtMaxSpeed) {
        var allReelsStopped = true

        for (reel = 0; reel < self.reels.length; reel++) {
            if (!self.reelsMoving[reel]) continue

            var stopSpot = self.stops[reel] * self.symbolHeight

            console.log('stop spot', stopSpot, 'position', self.positions[reel])

            if (stopSpot - self.positions[reel] <= self.speeds[reel]) {
                self.reelsMoving[reel] = false
                self.positions[reel] = stopSpot
            } else {
                allReelsStopped = false
            }
        }

        if (allReelsStopped) {
            clearInterval(self.timer)
            self.timer = null
        }
    }

    self.render()
}

Renderer.prototype.spin = function() {
    var self = this
    self.speeds = _.map(self.reels, function() { return 0 })
    self.reelsMoving = _.map(self.reels, function() { return true })
    self.stops = [0, 1, 2]
    self.timer = setInterval(_.bind(self.spinupTick, self), 1000 / 60)
}

http://jsfiddle.net/abrkn/yNtEE/7/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:01:39+00:00Added an answer on June 15, 2026 at 7:01 pm

    I forgot to call context.beginPath() before the clipping

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a first attempt to get some pixel manipulation done on a HTML5 canvas
First i selected images from photo library to ALAsset Library and after that i
I'm making my first game in canvas/JS and I'm running into an issue with
Just giving canvas a go for the first time with the intention of creating
I was studying live wallpaper. But first i have to study canvas animation. I
Hey there. I have to use the Google Chrome Frame to support certain HTML5-features
I gonna make a new kind of menu by canvas. This is my first
Im starting to learn canvas and i just hit my first frustrating situation, im
At first I was really excited about HTML5's canvas element. But It will not
I would like to have a Canvas that supports the DragDelta event. My first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.