Skip to content

Instantly share code, notes, and snippets.

@YingshanDeng
YingshanDeng / font-metrics.js
Created February 2, 2018 03:04
字体测量
function fontMetrics (family, size) {
this._family = family || (family = "Monaco, 'Courier New', Courier, monospace")
this._size = parseInt(size, 10) || (size = 12)
// Preparing container
var line = document.createElement('div')
var body = document.body
line.style.position = 'absolute'
line.style.whiteSpace = 'nowrap'
line.style.font = size + 'px ' + family
@YingshanDeng
YingshanDeng / hdpi-canvas-polyfill.js
Created February 2, 2018 02:32
hidp-canvas-polyfill
function hdpiCanvasPolyfill (canvas) {
var dpr = Math.max(1, window.devicePixelRatio || 1)
var logicalWidth = canvas.width
var logicalHeight = canvas.height
canvas.width = dpr * logicalWidth
canvas.height = dpr * logicalHeight
canvas.style.width = `${logicalWidth}px`
canvas.style.height = `${logicalHeight}px`
@YingshanDeng
YingshanDeng / simple-promise.js
Last active September 6, 2017 13:08
Implement Promise without The Promise Resolution Procedure
'use strict'
// The Promise States constants
const PENDING = 0
const FULFILLED = 1
const REJECTED = 2
export default class Promise {
// The constructor function
constructor (executor) {
@YingshanDeng
YingshanDeng / CSS3 Media Queries Template
Created March 21, 2017 01:47
CSS3 Media Queries template
/*
* Author: http://stuffandnonsense.co.uk/blog/about/hardboiled_css3_media_queries/
*/
/* Smartphones (portrait and landscape) ----------- */
@media only screen
and (min-device-width : 320px)
and (max-device-width : 480px) {
/* Styles */
}
<html>
<head>
<meta charset="UTF-8">
<title>object-observe</title>
<style>
</style>
</head>
-(UIImage *)fixOrientation:(UIImage *)aImage
{
if (aImage == nil)
{
return nil;
}
CGImageRef imgRef = aImage.CGImage;
CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);
CGAffineTransform transform = CGAffineTransformIdentity;
@YingshanDeng
YingshanDeng / iOS-CPU
Created May 22, 2015 11:35
获取 CPU 使用率
#import <mach/mach.h>
// 获取 CPU 使用率
float cpu_usage()
{
kern_return_t kr;
task_info_data_t tinfo;
mach_msg_type_number_t task_info_count;
task_info_count = TASK_INFO_MAX;
kr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)tinfo, &task_info_count);
@YingshanDeng
YingshanDeng / iOS-FindTopVC
Last active August 29, 2015 14:21
Find the current top view controller for your iOS application
- (UIViewController *)topViewController{
return [self topViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
- (UIViewController *)topViewController:(UIViewController *)rootViewController
{
if (rootViewController.presentedViewController == nil) {
return rootViewController;
}