theString.replace(/<.*?>/g, '');
A collection of handy code snippets in the languages that I use on a daily basis.
September 14, 2010
Javascript: strip HTML tags from a string
Here's a super simple RegEx to use when you want to be sure all html tags are removed from a string.
Labels:
html,
javascript,
js,
regex,
string
September 10, 2010
HTML/CSS on Android: rotation CSS difference between Android 2.1 and 2.2
Well, the splinternet is getting more interesting. As many developers settle on html as the most cross-functional platform, we're faced with ever more browsers and small differences between them. One that I just found is a difference in CSS positioning and rotation between the browsers on Android 2.1 and Android 2.2.
On my current project, I have a fancy UI that has an element constantly changing rotation and position using webkit transform CSS built with javascript. On Android 2.1, it worked fine as a 1-liner:
On my current project, I have a fancy UI that has an element constantly changing rotation and position using webkit transform CSS built with javascript. On Android 2.1, it worked fine as a 1-liner:
element.style.webkitTransform = "translate3d(" + xPos + "px, " + yPos + "px, 0px) rotate(" + rotation + "deg)";
But, on Android 2.2, the rotation stopped working. It seems that you can't have the translate3d and the rotate properties all set in the style.webkitTransform property. To fix the issue, I positioned using traditional absolute coordinates with the top and left CSS properties, and then used the webkitTransform property to do the rotation. There were a ton of special browser cases in my project to handle different things. Check out my platform detection class below to see how I handled a lot of special cases in one place.PlatformHelper = function ()
{
this.webkit_css_enabled = false;
this.animations_enabled = false;
this.is_android = false;
this.is_android21 = false;
this.is_android22 = false;
this.is_idevice = false;
this.is_touchscreen = false;
this.is_msie = false;
this.is_msie6 = false;
this.is_msie8 = false;
this.is_firefox = false;
return this;
};
PlatformHelper.prototype.init = function ()
{
// check for webkit positioning capability
if( navigator.userAgent.match(/iPhone/i) ) this.webkit_css_enabled = true;
else if( navigator.userAgent.match(/iPod/i) ) this.webkit_css_enabled = true;
else if( navigator.userAgent.match(/iPad/i) ) this.webkit_css_enabled = true;
else if( navigator.userAgent.match(/Chrome/i) ) this.webkit_css_enabled = true;
else if( navigator.userAgent.match(/Safari/i) ) this.webkit_css_enabled = true;
// check for certain platforms
if( navigator.userAgent.match(/Android/i) ) this.is_android = true;
if( navigator.userAgent.match(/Android 2.1/i) ) this.is_android21 = true;
if( navigator.userAgent.match(/Android 2.2/i) ) this.is_android22 = true;
if( navigator.userAgent.match(/MSIE/i) ) this.is_msie = true;
if( navigator.userAgent.match(/MSIE 6/i) ) this.is_msie6 = true;
if( navigator.userAgent.match(/MSIE 8/i) ) this.is_msie8 = true;
if( navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/iPad/i) ) this.is_idevice = true;
if( navigator.userAgent.match(/Firefox/i) ) this.is_firefox = true;
// special cases for touchscreens
if( this.is_android == true || this.is_idevice == true ) this.is_touchscreen = true;
// decide who sees animations
if( this.is_msie == true ) this.animations_enabled = false;
else this.animations_enabled = true;
};
PlatformHelper.prototype.updatePosition = function ( element, xPos, yPos, rotation )
{
if( !this.webkit_css_enabled || this.is_android22 )
{
element.style.left = xPos + 'px';
element.style.top = yPos + 'px';
element.style.MozTransform = 'rotate(' + rotation + 'deg)';
element.style.webkitTransform = 'rotate(' + rotation + 'deg)';
}
else
{
var new_transform = "translate3d(" + xPos + "px, " + yPos + "px, 0px) rotate(" + rotation + "deg)";
if( element.style.webkitTransform != new_transform ) // only apply style if not already in position
element.style.webkitTransform = new_transform;
}
};
September 8, 2010
Javascript / CSS animated text fire effect
A coworker sent me a funny example of a text-shadow CSS fire effect. I had a little time to kill, so I took the example and created an animated version using javascript. It's not very realistic, but it is highly silly:
Copy and paste the code into an html file to try it out:
Copy and paste the code into an html file to try it out:
<!DOCTYPE html>
<html>
<head>
<title>Fire</title>
<script type="text/javascript">
function fireText()
{
var FireColorStop = function( xPos, yPos, blur, color )
{
this.x = xPos;
this.y = yPos;
this.blur = blur;
this.color = color;
this.oscSpeed = Math.random() * Math.abs( yPos ) / 75;
this.oscIncrement = 0;
this.xOffset = 0;
this.yOffset = 0;
this.blurOffset = 0;
};
FireColorStop.prototype.oscillate = function()
{
this.oscIncrement += this.oscSpeed;
this.xOffset = Math.sin(this.oscIncrement) * this.blur / 3;
this.yOffset = Math.sin(this.oscIncrement) * 1;
this.blurOsc = this.blur + 10 + Math.sin(this.oscIncrement) * 3;
};
FireColorStop.prototype.getCSS = function()
{
return ( this.x + this.xOffset ) + 'px ' + ( this.y + this.yOffset ) + 'px ' + this.blurOsc + 'px ' + this.color;
};
// create objects for each color stop for independent animation
var fireColors = [ new FireColorStop(0, 0, 4, '#FFFFFF'),
new FireColorStop(0, -5, 4, '#FFFF33'),
new FireColorStop(2, -10, 6, '#FFDD33'),
new FireColorStop(-2,-15, 11, '#FF8800'),
new FireColorStop(2, -25, 18, '#FF2200')
];
var fps = 1000/30;
var text = document.getElementById('fireText');
// oscillate color stops and rebuild fire css
setInterval( function(){
var shadowCSS = '';
for( var i = 0; i < fireColors.length; i++ )
{
fireColors[i].oscillate();
shadowCSS += fireColors[i].getCSS();
if( i < fireColors.length - 1 )
shadowCSS += ', ';
}
text.style.textShadow = shadowCSS;
}, fps );
}
</script>
<style>
body, html {
background-color:black;
}
#fireText {
background-color:black;
position:absolute;
display:block;
width:100%;
height:300px;
line-height:300px;
color:white;
font-family: Arial, Verdana, sans-serif;
font-size:50px;
font-weight:bold;
text-align:center;
}
</style>
</head>
<body>
<div id="fireText">
Yeah Dude.
</div>
<script type="text/javascript">
fireText();
</script>
</body>
</html>
Labels:
animate,
code,
css,
drawing,
html,
javascript,
js,
setinterval,
textual
July 27, 2010
Android browser bug: pinch/zoom kills setTimeout()
I'm working on some cross-platform/mobile touch/mouse code for a fancy html/js UI, and everything's been working great, but when I pinch/zoom the web page in an Android browser, my setTimeout() calls stop running. To be safe, I recommend using setInterval() instead.
// before:
setTimeout( function() { runTimer(); } , 1000/30 );
function runTimer() {
// update graphics here
setTimeout( function() { runTimer(); } , 1000/30 );
}
// after:
setInterval( function(){ runTimer(); }, 1000/30 );
function runTimer() {
// update graphics here
}
I initially thought that my touch events (touchstart, touchmove, touchend) were randomly failing after zooming, because my custom motion code would completely break after running at a solid 30+ fps. It appears that this is a known bug in pre-2.2 (Froyo) Android web browsers: http://code.google.com/p/android/issues/detail?id=8566
Labels:
Android,
bug,
html,
javascript,
mobile,
pinch,
setinterval,
settimeout,
zoom
July 25, 2010
PHPFlicker: Get images by user's tag
I started using phpFlickr (http://phpflickr.com/), so I could use Flickr as a query-able backend for some of my images. I'm tagging images that I want to show up on certain pages of my site, but there wasn't an example for using tags on the phpFlickr examples page. So here's an example:
This script will display square thumbnails that link out to the largest available image size. This can easily by styled, augmented with a lightbox javascript, or customized via the phpFlickr search options.
Enjoy.
<?php
$api_key = "YOUR_API_KEY";
$user_id = "YOUR_USER_ID";
$secret = "YOUR_SECRET";
require_once("./php/flickr/phpFlickr.php");
$f = new phpFlickr($api_key, $secret);
$photos = $f->photos_search(array( "api_key"=>$api_key, "user_id"=>$user_id, "tags"=>"promo", "tag_mode"=>"any", "extras"=>"original_format,tags,url_o,description") );
// Loop through the photos and output the html
foreach( (array)$photos['photo'] as $photo )
{
// get original, or large if no original
if( isset( $photo['url_o'] ) )
echo '<a rel="lightbox[flickr]" title="'. $photo['title'].' - '. $photo['description'].'" href="'. $photo['url_o'] .'">';
else
echo '<a rel="lightbox[flickr]" title="'. $photo['title'].' - '. $photo['description'].'" href="'. $f->buildPhotoURL($photo, "large") .'">';
echo '<img border="0" alt="'.$photo[title].' - '. $photo['description'].'" title="'.$photo[title].' - '. $photo['description'].'" src="' . $f->buildPhotoURL($photo, "square") . '" />';
echo "</a>";
}
?>
Just replace YOUR_API_KEY, YOUR_USER_ID (something like: 38845956@N05), and YOUR_SECRET (something like 7fc67607bd5abc59). This type of search is "secure", meaning that you have to acquire a secret key via the Flickr API.This script will display square thumbnails that link out to the largest available image size. This can easily by styled, augmented with a lightbox javascript, or customized via the phpFlickr search options.
Enjoy.
Subscribe to:
Comments (Atom)
