用new Image().src作LOG统计的一个注意事项

本文揭示了一个使用JavaScript进行用户行为统计时容易忽视的问题:浏览器垃圾回收机制可能导致newImage().src方式发送的日志丢失。通过实际案例分析,介绍了如何规避这一问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载:http://blog.youkuaiyun.com/fudesign2008/article/details/6772108
用new Image().src作LOG统计的一个注意事项
2009-08-06 17:40

在大型网站做很多用户行为分析、产品的策划方案基本上都是通过分析用户的访问等信息而做出的,LOG信息的统计准确性会直接影响到产品的设计开发(比如搜索结果的先后排名rank值的产生等)。目前最常用的一个写LOG的方法就是用JavaScript脚本在网页里 new Image().src = "http://xxx.com/log?msg="+ msg; 这种统计方法基本上不会干扰用户的正常操作,虽然有LOG丢失的可能,但只要用得好,还是一种非常好的LOG统计回收的方案。

但是这种LOG回收手段有一个非常隐秘的隐患,事情是这样被发现的。今年3月份百度搜索结果页面上线了Suggestion功能升级版,在LOG统计中突然发现上线后的LOG总量比上线前的少了很多(10%以上),表现出来的现象就是LOG丢失了,没有回收到服务器中来。

工程师立即做了详细排除,从各种因素上确定LOG的大量丢失跟网页上线Suggestion有关,我们马上对Suggestion脚本进行地毯式排查,逐行分析代码,结论是这个脚本没有阻拦 new Image().src 的发包请求(至少表面上是这样的),这段脚本放在一个脚本闭包中,没有影响到全局变量/方法,也没有屏蔽干扰HTTP请求的因素。

我们又做了一个线下试验,同样的代码环境,在线下的测试环境中用这种 new Image().src 的手段,总共向Server端发送了10000个LOG数据,也没有发生LOG丢失,试验的结果也没有发现LOG丢失的原因。

但是在线上的环境中发生的就是有LOG数据丢失,原因不明,所以只能紧急下线这个Suggestion升级版。在这个脚本下线之后,LOG统计数据马上回归到“原正常”状态。从这个现象来看,也从另一个角度说明上线的新脚本确实对LOG统计有影响,头疼呀......

愚者千虑,必有一得!最后方知这个问题的原因是浏览器的垃圾回收机制

function c(q) {
    var p=window.document.location.href,sQ='',sV='';
    for(v in q){
        switch (v){
            case "title":sV=encodeURIComponent(q[v].replace(/<[^<>]+>/g,""));break;
            case "url":sV=escape(q[v]);break;
            default:sV=q[v]
        }
        sQ+=v+"="+sV+"&";
    }
    new Image().src = "http://s.baidu.com/w.gif?q=meizz&"+sQ+"path="+p+"&cid=9&t="+ new Date().getTime();
    return true;
}

这个 new Image() 对象没有赋给任何变量,在这个函数执行结束时,浏览器的垃圾回收机制对这种“无主”的对象是毫不客气的回收的,而正是这种回收行为导致了这个HTTP请求(异步的)没有发出,从而造成了LOG数据的丢失。那为什么上线一个脚本就会造成大量的LOG丢失呢?因为一个大脚本的运行回产生大量的“垃圾”,浏览器垃圾回收也会相应地更频繁的启动,从而造成LOG数据丢失。找到原因之后对症下药,把这个 new Image() 对象赋给一个全局有变量常期持有即可,相应的代码如下:

var n = "log_"+ (newDate()).getTime();
var c = window[n] =newImage();  //把new Image()赋给一个全局变量长期持有
c.onload = (c.onerror=function(){window[n] = null;});
c.src = "
http://s.baidu.com/w.gif?q=meizz"+ xxxx;
c = null;      //释放局部变量c

在这个统计代码上线之后,百度的搜索结果页面的LOG立即多出近10%,之后再上线其它的脚本也没有再出现LOG统计量的波动。

 

---------------------------------------------------------------------------------------------------------------------------------

 

局部变量下,测试连续发1000个log

IE6-7 发现log丢失,某些IE内核浏览器也发现log丢失

但在FF, chrome下未发现丢失

 

因此 保险的做法是用一个非局部变量持有

 

[javascript]  view plain copy
 
  1. var unique = (function () {  
  2.     var time= (new Date()).getTime()+'-', i=0;  
  3.     return function () {  
  4.        return time + (i++);  
  5.     }  
  6. })();  
  7.   
  8. var imgLog = function (url) {  
  9.     var data = window['imgLogData'] || (window['imgLogData'] = {});  
  10.     var img = new Image();  
  11.     var uid = unique();  
  12.     img.onload = img.onerror = function () {//销毁一些对象  
  13.         img.onload = img.onerror = null;  
  14.         img = null;  
  15.         delete data[uid];  
  16.     }  
  17.     img.src = url + '&_uid=' + uid;  
  18. };  


原文:http://blog.youkuaiyun.com/fudesign2008/article/details/6772108

 

<template> <view> <view class="map" id="mapDiv" style="width: 100%;height: 67vh;z-index: 0;position: relative;" :prop="dataOption" :change:prop="Trenderjs.initTMap" :change:renderTrigger="Trenderjs.handleMessage" :renderTrigger="triggerCounter"> </view> </view> </template> <script> export default { data() { return { triggerCounter: 0, isProcessing: false, message: null }; }, props: { dataOption: { type: String, default: '' }, }, methods: { renderjsData() { if (this.isProcessing) return; this.isProcessing = true; this.triggerCounter++; }, someMethodInVue(newVal) { this.$emit('childEvent', newVal); this.message = newVal; this.isProcessing = false; } } }; </script> <script module="Trenderjs" lang="renderjs"> var control; export default { data() { return { tk: '27657afc3318f8f2166fceba83427734', options: { lng: '104.397894', lat: '31.126855', }, Tmap: null, gisDataObj: {}, gisData: {}, geocode: null, gisPointLngLat: {}, // 定位优化相关变量 positionHistory: [], // 位置历史记录 maxHistorySize: 10, // 最大历史记录数 realTimeMarker: null, // 实时定位标记 realTimeLabel: null, // 实时定位标签 // 动画相关变量 animationTarget: null, // 动画目标位置 animationStep: 0, // 动画步数 animationTimer: null, // 动画计时器 animationDuration: 500, // 动画时长(ms) hasFitViewOnFirstLocation: false, // 方向相关变量 currentHeading: 0, // 当前设备方向(度数) compassWatchId: null, // 指南针监听ID isCompassAvailable: false, // 是否支持指南针 lastHeadingUpdate: 0, // 上次方向更新时间 headingFilterFactor: 0.1, // 方向滤波系数 // 修改 baseIcon 为更明显的箭头图标 baseIcon: 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA1MCA1MCIgd2lkdGg9IjUwIiBoZWlnaHQ9IjUwIj48Y2lyY2xlIGN4PSIyNSIgY3k9IjI1IiByPSIyMCIgZmlsbD0iIzNEODVGRiIgZmlsbC1vcGFjaXR5PSIwLjgiIHN0cm9rZT0iI2ZmZiIgc3Ryb2tlLXdpZHRoPSIyIi8+PHBhdGggZD0iTTI1IDVMMzUgMzVIMTV6IiBmaWxsPSIjZmZmIiBzdHJva2U9IiMwMDAiIHN0cm9rZS13aWR0aD0iMSIvPjwvc3ZnPg==', iconCache: {}, // 缓存不同角度的图标 lastRenderedHeading: null, // 上次渲染的角度 targetHeading: 0, // 目标方向角度 currentDisplayHeading: 0, // 当前显示的角度 animationFrameId: null, // 动画帧ID rotationSpeed: 0.2, // 增加旋转速度,范围0.1~0.2 // 视觉优化 markerOpacity: 1, // 标记透明度 markerScale: 1, // 标记缩放 } }, mounted() { if (!window.T) { const script = document.createElement('script') script.src = 'http://api.tianditu.gov.cn/api?v=4.0&tk=' + this.tk document.head.appendChild(script) script.onload = this.initChartsRender.bind() } else { this.initMap(); } this.initCompass(); // 预生成常用角度的图标,提高性能 this._preloadIcons(); // 初始化平滑方向 this.smoothHeading = 0; this.targetHeading = 0; this.currentDisplayHeading = 0; // 启动持续动画循环 this._startContinuousAnimation(); }, methods: { // 初始化地图 initMap() { const { lng, lat } = this.options; this.Tmap = new T.Map('mapDiv'); this.Tmap.centerAndZoom(new T.LngLat(lng, lat), 15); this.Tmap.setStyle("indigo"); // this.Tmap.disableDoubleClickZoom(); // this.Tmap.disableScrollWheelZoom(); // this.Tmap.disableInertia(); // this.Tmap.disableKeyboard(); this.geocode = new T.Geocoder(); // control = new T.Control.Zoom(); // this.Tmap.addControl(control); // control.setPosition(T_ANCHOR_BOTTOM_RIGHT); }, // 若没有加载地图方法 initChartsRender() { this.initMap(); }, // 初始化地图数据 initTMap(newValue, oldValue) { let that = this; // 清除旧的定位监听 that._watchId && plus.geolocation.clearWatch(that._watchId); // 启动新的定位监听 that._watchId = plus.geolocation.watchPosition( position => that._handlePosition(position), err => console.log('err', err), { provider: 'system', enableHighAccuracy: true, maximumAge: 0, timeout: 10000 } ); // 处理传入的地图数据 if (newValue) { setTimeout(function() { try { that.gisDataObj = JSON.parse(newValue); if (that.gisDataObj.hasOwnProperty('data') && that.gisDataObj.data) { that.gisData = JSON.parse(that.gisDataObj.data); if (that.gisData.hasOwnProperty('data') && that.gisData.data) { let gisPoint = JSON.parse(that.gisData.data); that.gisPointLngLat = gisPoint; // 设置中心点 let centerLng, centerLat; if (that.gisData.type == 'marker') { centerLng = gisPoint.lng; centerLat = gisPoint.lat; } else if (that.gisData.type == 'polyline' || that.gisData.type == 'polygon') { centerLng = gisPoint[0].lng; centerLat = gisPoint[0].lat; } else { centerLng = '104.397894'; centerLat = '31.126855'; } that.Tmap.centerAndZoom(new T.LngLat(centerLng, centerLat), 15); // 绘制地图元素 that._drawMapElements(); } } } catch (e) { console.error('数据解析错误:', e); } }, 1000); } }, // 处理定位点 - 核心优化 _handlePosition(position) { const coords = position.coords; // console.log("position: " + JSON.stringify(position)) const now = Date.now(); const newPoint = { lng: coords.longitude, lat: coords.latitude, accuracy: coords.accuracy, timestamp: now }; // 添加到历史记录 this.positionHistory.push(newPoint); if (this.positionHistory.length > this.maxHistorySize) { this.positionHistory.shift(); } // 使用卡尔曼滤波平滑位置 const smoothedPoint = this._kalmanFilter(newPoint); // 启动平滑动画 this._startAnimation(smoothedPoint); // 只在第一次定位时适配视野 if (!this.hasFitViewOnFirstLocation) { this._fitAllPoints(); this.hasFitViewOnFirstLocation = true; } }, // 卡尔曼滤波算法 _kalmanFilter(newPoint) { // 初始化状态 if (!this.kalmanState) { this.kalmanState = { x: newPoint.lng, y: newPoint.lat, vx: 0, // x方向速度 vy: 0, // y方向速度 P: [ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1] ], // 协方差矩阵 lastTime: newPoint.timestamp }; return newPoint; } // 时间差(秒) const dt = (newPoint.timestamp - this.kalmanState.lastTime) / 1000; this.kalmanState.lastTime = newPoint.timestamp; // 状态转移矩阵 const F = [ [1, 0, dt, 0], [0, 1, 0, dt], [0, 0, 1, 0], [0, 0, 0, 1] ]; // 过程噪声协方差矩阵 const Q = [ [Math.pow(dt, 4) / 4, 0, Math.pow(dt, 3) / 2, 0], [0, Math.pow(dt, 4) / 4, 0, Math.pow(dt, 3) / 2], [Math.pow(dt, 3) / 2, 0, Math.pow(dt, 2), 0], [0, Math.pow(dt, 3) / 2, 0, Math.pow(dt, 2)] ].map(row => row.map(val => val * 0.1)); // 噪声系数 // 预测状态 const x_pred = [ this.kalmanState.x + this.kalmanState.vx * dt, this.kalmanState.y + this.kalmanState.vy * dt, this.kalmanState.vx, this.kalmanState.vy ]; // 预测协方差 let P_pred = this._matrixMultiply(F, this.kalmanState.P); P_pred = this._matrixMultiply(P_pred, this._matrixTranspose(F)); P_pred = this._matrixAdd(P_pred, Q); // 测量矩阵 const H = [ [1, 0, 0, 0], [0, 1, 0, 0] ]; // 测量噪声协方差 const R = [ [Math.pow(newPoint.accuracy / 1000000, 2), 0], [0, Math.pow(newPoint.accuracy / 1000000, 2)] ]; // 卡尔曼增益 const S = this._matrixAdd( this._matrixMultiply(H, this._matrixMultiply(P_pred, this._matrixTranspose(H))), R ); const K = this._matrixMultiply( this._matrixMultiply(P_pred, this._matrixTranspose(H)), this._matrixInverse2x2(S) ); // 更新状态 const z = [newPoint.lng, newPoint.lat]; const y = [ z[0] - (H[0][0] * x_pred[0] + H[0][1] * x_pred[1] + H[0][2] * x_pred[2] + H[0][3] * x_pred[3]), z[1] - (H[1][0] * x_pred[0] + H[1][1] * x_pred[1] + H[1][2] * x_pred[2] + H[1][3] * x_pred[3]) ]; const x_upd = [ x_pred[0] + K[0][0] * y[0] + K[0][1] * y[1], x_pred[1] + K[1][0] * y[0] + K[1][1] * y[1], x_pred[2] + K[2][0] * y[0] + K[2][1] * y[1], x_pred[3] + K[3][0] * y[0] + K[3][1] * y[1] ]; // 更新协方差 const I = [ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1] ]; const KH = this._matrixMultiply(K, H); const I_KH = this._matrixSubtract(I, KH); const P_upd = this._matrixMultiply(I_KH, P_pred); // 更新状态 this.kalmanState = { x: x_upd[0], y: x_upd[1], vx: x_upd[2], vy: x_upd[3], P: P_upd, lastTime: newPoint.timestamp }; return { lng: x_upd[0], lat: x_upd[1], accuracy: newPoint.accuracy }; }, // 矩阵乘法 _matrixMultiply(A, B) { const result = []; for (let i = 0; i < A.length; i++) { result[i] = []; for (let j = 0; j < B[0].length; j++) { let sum = 0; for (let k = 0; k < A[0].length; k++) { sum += A[i][k] * B[k][j]; } result[i][j] = sum; } } return result; }, // 矩阵转置 _matrixTranspose(A) { return A[0].map((_, colIndex) => A.map(row => row[colIndex])); }, // 矩阵加法 _matrixAdd(A, B) { return A.map((row, i) => row.map((val, j) => val + B[i][j])); }, // 矩阵减法 _matrixSubtract(A, B) { return A.map((row, i) => row.map((val, j) => val - B[i][j])); }, // 2x2矩阵求逆 _matrixInverse2x2(A) { const det = A[0][0] * A[1][1] - A[0][1] * A[1][0]; return [ [A[1][1] / det, -A[0][1] / det], [-A[1][0] / det, A[0][0] / det] ]; }, // 开始平滑动画 _startAnimation(targetPoint) { // 取消之前的动画 if (this.animationTimer) { clearInterval(this.animationTimer); this.animationTimer = null; } // 设置目标位置 this.animationTarget = new T.LngLat(targetPoint.lng, targetPoint.lat); // 如果没有当前位置,直接设置 if (!this.currentPosition) { this.currentPosition = this.animationTarget; this._updateRealTimeMarker(targetPoint); return; } // 计算起点和终点 const startLng = this.currentPosition.getLng(); const startLat = this.currentPosition.getLat(); const endLng = this.animationTarget.getLng(); const endLat = this.animationTarget.getLat(); // 动画参数 const steps = Math.max(5, Math.min(20, this.animationDuration / 50)); const stepSize = 1 / steps; let progress = 0; // 启动动画 this.animationTimer = setInterval(() => { progress += stepSize; if (progress >= 1) { clearInterval(this.animationTimer); this.animationTimer = null; this.currentPosition = this.animationTarget; this._updateRealTimeMarker(targetPoint); // 定位点动画结束后,适配视野 // this._fitAllPoints(); return; } // 使用缓动函数使动画更平滑 const easedProgress = this._easeInOutCubic(progress); // 计算中间点 const currentLng = startLng + (endLng - startLng) * easedProgress; const currentLat = startLat + (endLat - startLat) * easedProgress; // 更新位置 this.currentPosition = new T.LngLat(currentLng, currentLat); this._updateRealTimeMarker({ lng: currentLng, lat: currentLat, accuracy: targetPoint.accuracy }); }, this.animationDuration / steps); }, // 缓动函数(三次缓动) _easeInOutCubic(t) { return t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2; }, // 更新实时定位标记 _updateRealTimeMarker(point) { if (!this.realTimeMarker) { // 使用初始方向创建标记 this.realTimeMarker = new T.Marker(new T.LngLat(point.lng, point.lat), { icon: new T.Icon({ iconUrl: this.baseIcon, iconSize: new T.Point(30, 30), className: 'direction-marker' }) }); this.Tmap.addOverLay(this.realTimeMarker); // 创建文本标签 this.realTimeLabel = new T.Label({ text: '实时定位', position: new T.LngLat(point.lng, point.lat), offset: new T.Point(-40, 10) }); this.Tmap.addOverLay(this.realTimeLabel); } else { // 更新位置 this.realTimeMarker.setLngLat(new T.LngLat(point.lng, point.lat)); this.realTimeLabel.setLngLat(new T.LngLat(point.lng, point.lat)); } // 应用视觉效果 if (this.realTimeMarker._icon) { this.realTimeMarker._icon.style.opacity = this.markerOpacity; this.realTimeMarker._icon.style.transform = `scale(${this.markerScale})`; } }, _fitAllPoints() { let points = []; // 点/线/面 if (this.gisData && this.gisPointLngLat) { if (this.gisData.type === 'marker') { points.push(new T.LngLat(this.gisPointLngLat.lng, this.gisPointLngLat.lat)); } else if (this.gisData.type === 'polyline' || this.gisData.type === 'polygon') { points = points.concat( this.gisPointLngLat.map(p => new T.LngLat(p.lng, p.lat)) ); } } // 当前定位点 if (this.currentPosition) { points.push(this.currentPosition); } // 过滤无效点 points = points.filter(p => p && typeof p.getLng === 'function' && typeof p.getLat === 'function'); if (points.length > 0) { this.Tmap.setViewport(points); } }, // 绘制地图元素(折线、多边形等) _drawMapElements() { if (!this.gisData || !this.gisPointLngLat) return; switch (this.gisData.type) { case 'marker': this._drawMarker(); break; case 'polyline': this._drawPolyline(); break; case 'polygon': this._drawPolygon(); break; } // 新增:绘制完后适配视野 this._fitAllPoints(); }, // 绘制标记点 _drawMarker() { const point = new T.LngLat(this.gisPointLngLat.lng, this.gisPointLngLat.lat); const marker = new T.Marker(point); this.Tmap.addOverLay(marker); const label = new T.Label({ text: this.gisData.name, position: point, offset: new T.Point(-40, 10) }); this.Tmap.addOverLay(label); }, // 绘制折线 _drawPolyline() { const points = this.gisPointLngLat.map(p => new T.LngLat(p.lng, p.lat) ); const polyline = new T.Polyline(points, { color: this.gisDataObj.color || '#FF0000', weight: 6, opacity: 0.7 }); this.Tmap.addOverLay(polyline); // 添加终点标签 const endPoint = points[points.length - 1]; const label = new T.Label({ text: this.gisData.name, position: endPoint, offset: new T.Point(-40, 10) }); this.Tmap.addOverLay(label); }, // 绘制多边形 _drawPolygon() { const points = this.gisPointLngLat.map(p => new T.LngLat(p.lng, p.lat) ); const polygon = new T.Polygon(points, { color: this.gisDataObj.color || '#1E90FF', weight: 3, fillColor: this.gisDataObj.color || '#1E90FF', fillOpacity: 0.3 }); this.Tmap.addOverLay(polygon); // 添加中心点标签 const center = this._calculateCenter(points); const label = new T.Label({ text: this.gisData.name, position: center, offset: new T.Point(-40, 10) }); this.Tmap.addOverLay(label); }, // 计算多边形中心点 _calculateCenter(points) { const sum = points.reduce((acc, point) => { acc.lng += point.getLng(); acc.lat += point.getLat(); return acc; }, { lng: 0, lat: 0 }); return new T.LngLat( sum.lng / points.length, sum.lat / points.length ); }, // RenderJS事件处理器 handleMessage(newVal, oldVal) { if (newVal !== oldVal && this.gisPointLngLat) { const geolocation = new T.Geolocation(); geolocation.getCurrentPosition(e => { const distance = this.getDistance( e.lnglat.lat, e.lnglat.lng, this.gisPointLngLat.lat, this.gisPointLngLat.lng ); this.$ownerInstance.callMethod('someMethodInVue', { lng: e.lnglat.lng, lat: e.lnglat.lat, distance: distance * 1000 }); }); } }, // 计算两点间距离(Haversine公式) getDistance(lat1, lng1, lat2, lng2) { const radLat1 = (lat1 * Math.PI) / 180.0; const radLat2 = (lat2 * Math.PI) / 180.0; const a = radLat1 - radLat2; const b = (lng1 * Math.PI) / 180.0 - (lng2 * Math.PI) / 180.0; const s = 2 * Math.asin( Math.sqrt( Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2) ) ); return s * 6378.137; // 返回公里数 }, // 预生成常用角度的图标 _preloadIcons() { for (let i = 0; i < 360; i += 5) { this._generateRotatedIcon(i) .then(rotatedIcon => { this.iconCache[i] = rotatedIcon; }); } }, // 初始化指南针 initCompass() { if (plus.orientation && plus.orientation.watchOrientation) { this.isCompassAvailable = true; this.compassWatchId = plus.orientation.watchOrientation( (orientation) => this._handleCompass(orientation), (err) => console.error('指南针错误:', err) ); } else { console.warn('设备不支持指南针功能'); this.isCompassAvailable = false; } }, _handleCompass(orientation) { // 获取方向并添加180度修正 let heading = orientation.magneticHeading; heading = (heading + 180) % 360; // 修正方向差180度的问题 // 只在变化足够大时更新方向,减少微小波动 if (Math.abs(this.currentHeading - heading) > 1) { this.currentHeading = heading; } // 记录时间戳 this.lastHeadingTimestamp = Date.now(); }, // 启动持续动画循环 _startContinuousAnimation() { // 清理已有动画 if (this.animationFrameId) { cancelAnimationFrame(this.animationFrameId); this.animationFrameId = null; } // 记录开始时间 let lastTimestamp = Date.now(); // 动画循环函数 const animate = (timestamp) => { // 计算时间增量(毫秒) const deltaTime = timestamp - lastTimestamp; lastTimestamp = timestamp; // 应用平滑滤波算法更新方向 if (this.isCompassAvailable) { // 计算最短路径的角度差 let diff = ((this.currentHeading - this.smoothHeading + 540) % 360) - 180; // 根据时间增量调整滤波系数,确保动画速度一致 const factor = Math.min(1, this.headingFilterFactor * (deltaTime / 16.67)); // 平滑更新方向 this.smoothHeading = (this.smoothHeading + diff * factor + 360) % 360; // 设置目标角度 this.targetHeading = this.smoothHeading; // 检测快速旋转,添加视觉反馈 if (Math.abs(diff) > 15) { // 快速旋转时添加缩放动画 this._addRotationVisualEffect(); } } // 更新图标方向 this._updateIconRotation(deltaTime); // 继续动画循环 this.animationFrameId = requestAnimationFrame(animate); }; // 启动动画循环 this.animationFrameId = requestAnimationFrame(animate); }, // 添加旋转视觉效果 _addRotationVisualEffect() { // 快速旋转时短暂放大图标并调整透明度 this.markerScale = 1.2; this.markerOpacity = 0.9; // 恢复动画 setTimeout(() => { this.markerScale = 1; this.markerOpacity = 1; }, 200); }, // 更新图标旋转 - 优化版本 _updateIconRotation(deltaTime) { // 如果没有标记,直接返回 if (!this.realTimeMarker) return; // 计算最短路径的角度差 let diff = ((this.targetHeading - this.currentDisplayHeading + 540) % 360) - 180; // 如果角度差很小,直接设置目标角度 if (Math.abs(diff) < 0.5) { this.currentDisplayHeading = this.targetHeading; } else { // 根据时间增量调整旋转速度,确保动画速度一致 const maxDelta = this.rotationSpeed * (deltaTime / 16.67); const delta = Math.min(maxDelta, Math.abs(diff)) * Math.sign(diff); // 更新当前显示角度 this.currentDisplayHeading = (this.currentDisplayHeading + delta + 360) % 360; } // 更新图标方向 this._updateMarkerDirection(this.currentDisplayHeading); }, // 丝滑旋转动画 - 优化版本 _animateMarkerRotation() { // 清理已有动画 if (this.animationFrameId) { cancelAnimationFrame(this.animationFrameId); this.animationFrameId = null; } // 记录开始时间 const startTime = Date.now(); // 使用基于时间的动画,确保在不同帧率下速度一致 const animate = () => { // 计算动画进度 const elapsed = Date.now() - startTime; const deltaTime = elapsed / 16.67; // 假设60fps的基准时间 // 计算最短路径的角度差 let diff = ((this.targetHeading - this.currentDisplayHeading + 540) % 360) - 180; // 如果角度差很小,直接设置目标角度并停止动画 if (Math.abs(diff) < 0.5) { this.currentDisplayHeading = this.targetHeading; this._updateMarkerDirection(this.currentDisplayHeading); this.animationFrameId = null; return; } // 使用基于时间的旋转步长,确保动画速度一致 const delta = diff * Math.min(1, this.rotationSpeed * deltaTime); // 更新当前显示角度 this.currentDisplayHeading = (this.currentDisplayHeading + delta + 360) % 360; // 更新marker方向 this._updateMarkerDirection(this.currentDisplayHeading); // 继续动画 this.animationFrameId = requestAnimationFrame(animate); }; this.animationFrameId = requestAnimationFrame(animate); }, // 更新marker方向(始终传递角度) _updateMarkerDirection(heading) { // 四舍五入到最接近的5度(与预生成的图标匹配) const roundedHeading = Math.round(heading / 5) * 5; if (this.lastRenderedHeading === roundedHeading) return; // 使用缓存的图标 if (this.iconCache[roundedHeading]) { this._setMarkerIcon(this.iconCache[roundedHeading]); this.lastRenderedHeading = roundedHeading; } else { // 如果图标不存在,生成并缓存(应该很少发生) this._generateRotatedIcon(roundedHeading) .then(rotatedIcon => { this.iconCache[roundedHeading] = rotatedIcon; this._setMarkerIcon(rotatedIcon); this.lastRenderedHeading = roundedHeading; }); } }, // 生成旋转后的图标 _generateRotatedIcon(heading) { return new Promise((resolve) => { const size = 30; const canvas = document.createElement('canvas'); canvas.width = size; canvas.height = size; const ctx = canvas.getContext('2d'); const img = new Image(); img.src = this.baseIcon; img.onload = () => { // 清除画布 ctx.clearRect(0, 0, size, size); // 移动到中心点 ctx.translate(size / 2, size / 2); // 旋转画布 ctx.rotate((heading * Math.PI) / 180); // 绘制图像(注意调整位置) ctx.drawImage(img, -size / 2, -size / 2, size, size); // 恢复画布状态 ctx.setTransform(1, 0, 0, 1, 0, 0); // 获取Base64数据 resolve(canvas.toDataURL('image/png')); }; }); }, // 设置标记图标 _setMarkerIcon(iconUrl) { if (!this.realTimeMarker) return; this.realTimeMarker.setIcon(new T.Icon({ iconUrl: iconUrl, iconSize: new T.Point(30, 30), className: 'direction-marker' })); }, }, // 在组件销毁时清理 beforeDestroy() { // 清除位置监听 this._watchId && plus.geolocation.clearWatch(this._watchId); // 清除指南针监听 if (this.compassWatchId && plus.orientation.clearWatch) { plus.orientation.clearWatch(this.compassWatchId); } this.iconCache = {}; // 清理动画循环 if (this.animationFrameId) { cancelAnimationFrame(this.animationFrameId); this.animationFrameId = null; } } }; </script> 不对,现在的代码中,旋转还是一顿一顿的和市面上的地图不一样
07-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值