如何从滚动条计算视口? [英] How to compute viewport from scrollbar?

查看:36
本文介绍了如何从滚动条计算视口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能很简单,但由于某种原因我无法破解(实际上是因为我可悲的是数学太糟糕了!).

This is probably straight forward enough but for some reason I just can't crack it (well actually its because I'm terrible at maths sadly!).

我有一个视口,一次可显示800个像素,我想在大约2400像素宽的虚拟像素上左右移动视口.

I have a viewport that shows 800 pixels at a time and I want to move it left and right over a virtual number of pixels say 2400 pixels wide.

我使用滚动条的相对位置(0-1)来确定视口应该已经移动到的虚拟像素.

I use the relative position of a scrollbar (0-1) to determine the virtual pixel that the viewport should have moved to.

我经历了一个循环n = 800.我想从滑块告诉我的位置开始读取一组绘图点,并从该点开始迭代下一个800点.

I have a loop n = 800 that I run through. I want to start reading an array of plottin points from the position the slider tells me and iterate through the next 800 points from that point.

我遇到的问题是,当滚动条一直向右移动并且其相对位置= 1我的读取点= 2400 .....在某种程度上是正确的.但这使我绘制点的读取位置移到了最后一个点,而当我开始迭代时,我已经没有点可读取了.

The problem I have is when my scrollbar is all the way to the right and its relative position = 1 my readpoint = 2400..... which in a way is right. But that makes my read position of plotting points go to the last point and when I begin my iteration I have no points left to read.

根据我移动的800像素宽的滑动框类目,在这种情况下,框的左点应为1600,而其右点应为2400

Based on my moving an 800 pixel wide sliding box analagy the left point of the box should be 1600 in this case and its right point should be 2400

我该如何实现?

我需要某种值映射公式吗?

Do I need some sort of value mapping formula?

        private function calculateShuttleRelativePosition():void
        {

            var _x:Number=_scrollthumb.x
            _sliderCenter=_x + (_scrollthumb.width / 2)
            _sliderRange=_scrollbar.width - _scrollthumb.width;
            _slider_rel_pos=(_sliderCenter - _sliderCenterMinLeft) / _sliderRange

        }



pixel = slider relative position * 2400
readpoint= pixel

推荐答案

滚动条的范围应该是最左边像素可以拍摄的范围,而不是所有像素的范围.

The range of your scrollbar should be the range that the left most pixel can take, not the range of all the pixels.

因此您的范围将是1600(2400-800),而不是2400.这是确定偏移量应使用的比例因子.

So your range would be 1600 (2400 - 800), not 2400 alone. This is the scaling factor you should apply to determine your offset.

警告一语总是要警惕这些事情中的一个错误.由于您的条形范围从0到1,如果您不小心,则输出的像素范围将在0到800之间,如果您不注意:),这可能会使数组溢出:

As a word of warning always be on the lookout for off by one errors in these kinds of things. Since your bar ranges from 0 to 1 your outputted pixels will range from 0 to 800 if you are not careful, which could overflow your array if you don't watch out :).

这篇关于如何从滚动条计算视口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆