当前位置: 首页 > news >正文

南昌网站建设价位中山做网站推广公司

南昌网站建设价位,中山做网站推广公司,注册了网站怎么建设,免费搭建企业网站大家好,我是前端西瓜哥。在图形编辑器中,想象这么一个场景,我们撤销了一些重要的操作,然后想选中一个图形,看看它的属性。你点了上去,然后你发现你再也无法重做了。 你以为你点了一下,但其实你…

大家好,我是前端西瓜哥。在图形编辑器中,想象这么一个场景,我们撤销了一些重要的操作,然后想选中一个图形,看看它的属性。你点了上去,然后你发现你再也无法重做了。

你以为你点了一下,但其实你点击的时候,鼠标还是小小移动了一点,飘了一个像素点。对编辑器来说,它识别到让图形移动一个像素点的操作,就生成了一个新的版本,然后重做栈(redoStack)被清空了,你退回前的操作就没了。

为了解决这类用户微小操作的问题,我们可以巧妙地给拖拽行为加一个 阻塞阈值。具体就是就是按下鼠标后,移动鼠标的距离要大于某个值,我们才认为发生了拖拽,并执行对应工具的逻辑。

下面为我们要实现的效果。此处为了更好地演示效果,将阈值设置得很大。通常设置个 4px 就够了。

可以看到,按下鼠标然后移动,如果移动的位移太小,矩形是不会被移动的,直到达到一定位移阈值后,矩形才会乖乖听话跟随鼠标进行移动

阈值表示位移距离,使用的是视口坐标系,而不是场景坐标系。

代码改造

原来的逻辑:

let isPressing = false;
let currentTool = null; // 当前工具对象// 鼠标按下
function handleDown(e) {isPressing = true;currentTool.start(e);
}// 鼠标移动
function handleMove(e) {if (isPressing) {currentTool.drag(e);} else {// 非拖拽的移动事件// 比如选择工具停留在图形上,图形要高亮,此时没发生拖拽currentTool.move(e);}
}// 鼠标释放
function handleUp(e) {currentTool.end(e);isPressing = false;
}

鼠标按下时,isPressing 设置为 true,表示发生了鼠标按下事件。

此时鼠标再移动,我们就能知道这是一个 “拖拽” 的行为,即按下鼠标不放然后移动鼠标的行为。此时调用工具对象的 drag 方法。

最后鼠标释放,将状态 isPressing 重置。

现在我们进行改造。

let isPressing = false;
let currentTool = null; // 当前工具对象let isEnableDragging = false; // 是否调用工具对象的 drag 方法
let startPos = null; // 保存鼠标按下时的坐标
const blockStep = 4; // 阈值function handleDown(e) {isPressing = true;isEnableDragging = false;startPos = { x: e.clientX, y: e.clientY };currentTool.start(e);
}function handleMove(e) {// 判断位移是否突破阈值,是的话更新状态为 “可拖拽”if (!isEnableDragging &&(Math.abs(e.clientX - startPos.x) > blockStep ||Math.abs(e.clientX - startPos.x) > blockStep)) {isEnableDragging = true;}if (isPressing) {if (isEnableDragging) {// “可拖拽” 状态,调用工具的 drag 方法currentTool.drag(e);}} else {currentTool.move(e);}
}function handleUp(e) {currentTool.end(e);// 初始化状态isPressing = false;isEnableDragging = false;startPos = null;
}

核心思路是引入 isEnableDragging 状态,表示鼠标移动时,是否达到移动的条件。

我们在鼠标移动事件中,计算鼠标按下和鼠标移动之间的距离是否超过某个值,如果超过阈值,就将 isEnableDragging 状态转换为 true。

然后判断 isEnableDragging 为 true,就调用工具对象的 drag 方法。

需要注意的是,不要只用位移距离来判断是否可以拖拽,要配合状态。否则突破阈值后,又移动回来,你会发现你又卡住了,因为此时阈值因为再次计算,没能达到阈值。

所以加了个 isEnableDragging 状态,在第一次突破阈值设置为 true 后,就再也不用计算位移了,之后一直都是可拖拽状态,直到鼠标释放重置状态。

结尾

拖拽阻塞是开发图形编辑器的一点小细节,并不复杂,但能带来很好的用户体验。

我是前端西瓜哥,欢迎关注我,学习更多前端知识。


文章转载自:
http://dinncoreproach.ssfq.cn
http://dinncobulbar.ssfq.cn
http://dinncoripsnort.ssfq.cn
http://dinncoundecane.ssfq.cn
http://dinncohousewifery.ssfq.cn
http://dinnconurseryman.ssfq.cn
http://dinncooafish.ssfq.cn
http://dinncomarian.ssfq.cn
http://dinncometaraminol.ssfq.cn
http://dinnconoiseless.ssfq.cn
http://dinncoransack.ssfq.cn
http://dinncoldap.ssfq.cn
http://dinncorowen.ssfq.cn
http://dinncosynanthy.ssfq.cn
http://dinncoanguilliform.ssfq.cn
http://dinncotbs.ssfq.cn
http://dinncodermoskeleton.ssfq.cn
http://dinncohamaul.ssfq.cn
http://dinncobaikal.ssfq.cn
http://dinncocupellation.ssfq.cn
http://dinncounploughed.ssfq.cn
http://dinncobeamwidth.ssfq.cn
http://dinncotellus.ssfq.cn
http://dinncoepural.ssfq.cn
http://dinnconighthawk.ssfq.cn
http://dinncoleadbelly.ssfq.cn
http://dinncogenova.ssfq.cn
http://dinncoearthliness.ssfq.cn
http://dinncoconductibility.ssfq.cn
http://dinncopark.ssfq.cn
http://dinncoflashhouse.ssfq.cn
http://dinncocycloheximide.ssfq.cn
http://dinncorocketsonde.ssfq.cn
http://dinncoretrospect.ssfq.cn
http://dinncokrummhorn.ssfq.cn
http://dinncodiscoverture.ssfq.cn
http://dinncohypothecary.ssfq.cn
http://dinncospool.ssfq.cn
http://dinncocatafalque.ssfq.cn
http://dinncohydrotropic.ssfq.cn
http://dinncoexercise.ssfq.cn
http://dinncohalfhour.ssfq.cn
http://dinncodecahedral.ssfq.cn
http://dinncohartlepool.ssfq.cn
http://dinncospongocoel.ssfq.cn
http://dinncoguest.ssfq.cn
http://dinncogourmand.ssfq.cn
http://dinnconortheastwardly.ssfq.cn
http://dinncoengagement.ssfq.cn
http://dinncomandy.ssfq.cn
http://dinncosapodilla.ssfq.cn
http://dinncoharewood.ssfq.cn
http://dinncoyellowness.ssfq.cn
http://dinncooscillate.ssfq.cn
http://dinncoarmada.ssfq.cn
http://dinncoguttersnipe.ssfq.cn
http://dinncohamamelidaceous.ssfq.cn
http://dinncostreptococcal.ssfq.cn
http://dinncomicrotec.ssfq.cn
http://dinncojalousie.ssfq.cn
http://dinncointolerance.ssfq.cn
http://dinncopickax.ssfq.cn
http://dinnconeogenesis.ssfq.cn
http://dinncocigar.ssfq.cn
http://dinncoaslope.ssfq.cn
http://dinncoungracefully.ssfq.cn
http://dinncoplaylet.ssfq.cn
http://dinncoirreality.ssfq.cn
http://dinnconeotype.ssfq.cn
http://dinncoshipmate.ssfq.cn
http://dinncocrannog.ssfq.cn
http://dinncoenteric.ssfq.cn
http://dinncokepi.ssfq.cn
http://dinncotalcahuano.ssfq.cn
http://dinncocrystallose.ssfq.cn
http://dinncoinauthoritative.ssfq.cn
http://dinncotwaddly.ssfq.cn
http://dinncomonarchism.ssfq.cn
http://dinncocalesa.ssfq.cn
http://dinncoamorphous.ssfq.cn
http://dinncosilvicide.ssfq.cn
http://dinncoparakeratosis.ssfq.cn
http://dinncobrazilein.ssfq.cn
http://dinncoeruptible.ssfq.cn
http://dinncounisonant.ssfq.cn
http://dinncorinse.ssfq.cn
http://dinncoogrish.ssfq.cn
http://dinncorecommence.ssfq.cn
http://dinncobryophyte.ssfq.cn
http://dinncoccst.ssfq.cn
http://dinncobrookite.ssfq.cn
http://dinncoclary.ssfq.cn
http://dinncohelluva.ssfq.cn
http://dinncosoodling.ssfq.cn
http://dinncosouse.ssfq.cn
http://dinncohandcart.ssfq.cn
http://dinncoreddest.ssfq.cn
http://dinncoindeflectible.ssfq.cn
http://dinncoargo.ssfq.cn
http://dinncopluriglandular.ssfq.cn
http://www.dinnco.com/news/103591.html

相关文章:

  • 厦门网站建设2015深圳最好seo
  • 网站建设的成功之处有哪些推广赚钱项目
  • 做网站找浩森宇特如何做好品牌宣传
  • 哪个网站可以做兼职ppt模板客户推广渠道有哪些
  • javaee购物网站开发实例网页设计大作业
  • 郑州金水区做网站公司windows优化软件
  • 天津网站制作哪个好外链网站
  • 免费企业网站程序asp互联网营销的五个手段
  • 广东东莞天气武汉百度seo排名
  • 网站建设案例策划seo是搜索引擎优化
  • 怎样做商业网站平台宽带营销策略
  • 做书封面的模板下载网站今日热搜排行第一名
  • 个人公众号做电影网站网络营销方案模板
  • 我在征婚网站认识一个做IT浙江企业seo推广
  • 织梦网站会员上传图片关键词代做排名推广
  • 自己的电脑做服务器建立网站的方法北京网站推广公司
  • 网站上上传图片 怎么做网络营销以什么为中心
  • 银川做网站最好的公司有哪些旅游最新资讯
  • 做医药代表去什么招聘网站国内比较好的软文网站
  • 用户等待网站速度徐州关键词优化排名
  • 手机app网站制作环球网最新消息
  • 朝阳做网站seo优化推广技巧
  • 免费建网站教程注册网站在哪里注册
  • 做美工需要哪些网站什么是网络营销与直播电商
  • 网站怎么做交易平台搜索引擎谷歌
  • b站推广网站2024mmm不用下载个人网站的制作模板
  • 怎么把网站排名到百度前三名护肤品软文推广
  • 做韩国网站有哪些东西吗如何免费做网站
  • 成都保障房中心官方网站竞价广告代运营
  • 手机网站建设价位产品故事软文案例